Skip to content

Commit 140bb35

Browse files
committed
resolve some warning and optimize synchronized example
1 parent 7542e78 commit 140bb35

File tree

5 files changed

+79
-62
lines changed

5 files changed

+79
-62
lines changed

src/main/java/com/example/concurrency/atomic/SimulatedCAS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* 描述:
5-
* SimulatedCAS
5+
* SimulatedCAS 模拟cas操作
66
*
77
* @author zed
88
* @since 2019-06-18 2:09 PM
@@ -26,7 +26,7 @@ void addOne(int newValue){
2626
* @param newValue 更新值
2727
* @return 旧值
2828
*/
29-
synchronized int cas(
29+
private synchronized int cas(
3030
int expect, int newValue){
3131
// 读目前 count 的值
3232
int curValue = count;

src/main/java/com/example/concurrency/contentSwitch/ContentSwitchTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.example.concurrency.contentSwitch;
22

3+
import com.example.concurrency.threadPool.ThreadPoolBuilder;
4+
5+
import java.util.concurrent.ThreadPoolExecutor;
6+
37
/**
48
* @author zed
59
* 上下文切换测试类
@@ -14,33 +18,35 @@
1418
*/
1519
public class ContentSwitchTest {
1620
private static final long COUNT = 10000000000L;
17-
public static void main(String[] args) throws InterruptedException{
21+
22+
private static ThreadPoolExecutor threadPoolExecutor = ThreadPoolBuilder.fixedPool().setPoolSize(1).build();
23+
24+
public static void main(String[] args){
1825
concurrency();
1926
serial();
2027
}
2128

2229

23-
private static void concurrency() throws InterruptedException{
30+
private static void concurrency(){
2431
long start = System.currentTimeMillis();
25-
Thread thread = new Thread(() -> {
32+
threadPoolExecutor.execute(() -> {
2633
int a =0;
2734
for (long i =0;i < COUNT;i++){
2835
a+=5;
2936
}
3037
System.out.println(a);
3138
});
32-
thread.start();
3339
int b = 0;
3440
for(long i = 0;i < COUNT;i ++){
3541
b--;
3642
}
37-
thread.join();
43+
threadPoolExecutor.shutdown();
3844
long time = System.currentTimeMillis() - start;
3945
System.out.println("concurrency :" +time +"ms,b="+b);
4046

4147
}
4248

43-
private static void serial() throws InterruptedException{
49+
private static void serial(){
4450
long start = System.currentTimeMillis();
4551
int a = 0;
4652
for(long i = 0;i < COUNT;i++){

src/main/java/com/example/concurrency/deadLock/DeadLockTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.example.concurrency.deadLock;
22

3+
import com.example.concurrency.threadPool.ThreadPoolBuilder;
34
import com.example.concurrency.util.ThreadDumpHelper;
45
import com.example.concurrency.util.ThreadUtil;
56

7+
import java.util.concurrent.ThreadPoolExecutor;
8+
69

710
/**
811
* @author zed
@@ -16,19 +19,21 @@
1619
*/
1720
public class DeadLockTest {
1821
private ThreadDumpHelper dumpHelper = new ThreadDumpHelper();
22+
private static ThreadPoolExecutor threadPoolExecutor = ThreadPoolBuilder.fixedPool().setPoolSize(2).setThreadNamePrefix("死锁测试线程").build();
23+
1924
/**
2025
* 事实上String 对象不建议当成锁的对象(常量池的存在会导致锁重复)
2126
* 这里故意将A、B放入堆中以示问题
2227
*/
23-
private static String A = new String("A");
24-
private static String B = new String("B");
28+
private static final String A = new String("A");
29+
private static final String B = new String("B");
2530

2631
public static void main(String[] args) {
2732
new DeadLockTest().deadLock();
2833
}
2934

3035
private void deadLock() {
31-
Thread t1 = new Thread(() -> {
36+
threadPoolExecutor.execute(() -> {
3237
synchronized (A){
3338
ThreadUtil.sleep(2);
3439
synchronized (B){
@@ -37,17 +42,14 @@ private void deadLock() {
3742
}
3843
});
3944

40-
Thread t2 = new Thread(() -> {
45+
threadPoolExecutor.execute(() -> {
4146
synchronized (B) {
4247
synchronized (A) {
4348
System.out.println("2");
4449
}
4550
}
4651
});
47-
t1.setName("线程1");
48-
t2.setName("线程2");
49-
t1.start();
50-
t2.start();
52+
threadPoolExecutor.shutdown();
5153
// //获取最终状态,可能会出现线程T1 状态为TIMED_WAITING 状态(因为sleep)
5254
ThreadUtil.sleep(2);
5355
dumpHelper.tryThreadDump();

src/main/java/com/example/concurrency/synchronizedEx/SynchronizedConnection.java

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.example.concurrency.synchronizedEx;
22

3+
import com.example.concurrency.threadPool.ThreadPoolBuilder;
34
import com.example.concurrency.util.ThreadDumpHelper;
45
import com.example.concurrency.util.ThreadUtil;
56

7+
import java.util.Scanner;
8+
import java.util.concurrent.ThreadPoolExecutor;
9+
610
/**
711
* 描述:
812
* 保护有关联关系的多个资源 银行转帐问题
@@ -11,6 +15,8 @@
1115
* @since 2019-06-13 1:13 PM
1216
*/
1317
public class SynchronizedConnection {
18+
private static ThreadPoolExecutor threadPoolExecutor = ThreadPoolBuilder.fixedPool().setPoolSize(2).setThreadNamePrefix("sync测试线程").build();
19+
1420
/**
1521
* 线程不安全
1622
*/
@@ -36,10 +42,17 @@ void transfer(
3642
/**
3743
* 直接对转账方法做同步方式
3844
* 锁对象为this 而target对象资源不受保护
39-
*
4045
*/
4146
static class SynchronizedTransferAccount {
47+
/**
48+
* 用于测试尽量保证并发
49+
*/
50+
private volatile boolean flag = true;
51+
private void breakLoop(){
52+
this.flag =false;
53+
}
4254
private int balance;
55+
4356
private SynchronizedTransferAccount(int balance){
4457
this.balance =balance;
4558
}
@@ -48,29 +61,47 @@ private SynchronizedTransferAccount(int balance){
4861
* @param target 目标账户
4962
* @param amt 转账数量
5063
*/
51-
synchronized void transfer(
52-
SynchronizedTransferAccount target, int amt){
53-
if (this.balance > amt) {
64+
synchronized void transfer(SynchronizedTransferAccount target, int amt){
65+
66+
if (this.balance >= amt) {
67+
while (flag){
68+
69+
}
70+
System.out.println(Thread.currentThread().getName()+"this amount"+this.balance+"target amount:"+target.balance);
5471
this.balance -= amt;
5572
target.balance += amt;
56-
73+
System.out.println(Thread.currentThread().getName()+"this amount"+this.balance+"target amount:"+target.balance);
5774
}
75+
5876
}
5977
public static void main(String[] args){
6078
SynchronizedTransferAccount a = new SynchronizedTransferAccount(200);
6179
SynchronizedTransferAccount b = new SynchronizedTransferAccount(200);
6280
SynchronizedTransferAccount c = new SynchronizedTransferAccount(200);
81+
6382
//账户a给b转账100
64-
Thread t1 = new Thread(() -> {
83+
threadPoolExecutor.execute(() -> {
6584
a.transfer(b,100);
6685
});
6786
//账户b转账给c 100
68-
Thread t2 = new Thread(() -> {
87+
threadPoolExecutor.execute(() -> {
6988
b.transfer(c,100);
7089
});
71-
t1.start();
72-
t2.start();
73-
// 本地测试不好模拟出同时进入临界区的场景
90+
91+
Scanner scanner = new Scanner(System.in);
92+
while (scanner.hasNext()){
93+
String v = scanner.next();
94+
if("1".equals(v)){
95+
a.breakLoop();
96+
b.breakLoop();
97+
c.breakLoop();
98+
break;
99+
}
100+
}
101+
threadPoolExecutor.shutdown();
102+
System.out.println("a:"+a.balance);
103+
System.out.println("b:"+b.balance);
104+
System.out.println("c:"+c.balance);
74105
// 线程t1 和t2 如果同时进入临界区则 读取到的账户b的余额都是200 因此结果会因为t1 和t2的执行顺序变成100 或者300
75106
}
76107
}
@@ -151,27 +182,17 @@ void transfer(DeadLockAccount target, int amt){
151182
public static void main(String[] args) {
152183
DeadLockAccount a = new DeadLockAccount();
153184
DeadLockAccount b = new DeadLockAccount();
154-
Thread t1 = new Thread(() -> {
185+
threadPoolExecutor.execute(() -> {
155186
a.transfer(b,100);
156187
});
157188

158-
Thread t2 = new Thread(() -> {
189+
threadPoolExecutor.execute(() -> {
159190
b.transfer(a,100);
160191
});
161-
t1.setName("T1");
162-
t2.setName("T2");
163-
t1.start();
164-
t2.start();
165-
//获取最终状态,可能会出现线程T1 状态为TIMED_WAITING 状态(因为sleep)
166-
ThreadUtil.sleep(2);
192+
threadPoolExecutor.shutdown();
167193
dumpHelper.tryThreadDump();
168194
}
169195
}
170196

171-
172-
173-
174-
175-
176197
}
177198

src/main/java/com/example/concurrency/visibility/Visibility.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.example.concurrency.threadPool.ThreadPoolBuilder;
44
import com.example.concurrency.util.ThreadUtil;
5-
import lombok.extern.slf4j.Slf4j;
65

76
import java.util.concurrent.ThreadPoolExecutor;
87

@@ -13,10 +12,11 @@
1312
* @author zed
1413
* @since 2019-06-13 9:05 AM
1514
*/
16-
@Slf4j
1715
public class Visibility {
1816
private static long count = 0;
19-
private void add10K() {
17+
private static ThreadPoolExecutor threadPoolExecutor = ThreadPoolBuilder.fixedPool().build();
18+
19+
private void add10k() {
2020
int idx = 0;
2121
while(idx++ < 10000) {
2222
count += 1;
@@ -30,38 +30,26 @@ private void add10K() {
3030
public static void main(String[] args) {
3131
// System.out.println(calc());
3232

33-
log.info("我开始了");
34-
ThreadPoolExecutor threadPoolExecutor = ThreadPoolBuilder.fixedPool().build();
33+
System.out.println("start");
3534
//线程开始
3635
threadPoolExecutor.execute(() -> {
3736
while(flag){
3837

3938
}
40-
log.info("我退出了");
39+
System.out.println("stop");
4140

4241
});
4342
ThreadUtil.sleep(100);
4443
flag = false;
4544
}
4645
private static long calc(){
4746
final Visibility visibility = new Visibility();
48-
// 创建两个线程,执行 add() 操作
49-
Thread th1 = new Thread(()->{
50-
visibility.add10K();
51-
});
52-
Thread th2 = new Thread(()->{
53-
visibility.add10K();
54-
});
55-
// 启动两个线程
56-
th1.start();
57-
th2.start();
58-
// 等待两个线程执行结束
59-
try{
60-
th1.join();
61-
th2.join();
62-
}catch (InterruptedException e){
63-
Thread.currentThread().interrupt();
64-
}
47+
threadPoolExecutor.execute(visibility::add10k);
48+
threadPoolExecutor.execute(visibility::add10k);
49+
/*
50+
* 调用shuntDown保证线程执行完毕
51+
*/
52+
threadPoolExecutor.shutdown();
6553
return count;
6654

6755
}

0 commit comments

Comments
 (0)