Skip to content

Commit ef43d0d

Browse files
committed
原子类也并不完全安全:方法与方法之间的调用有可能顺序错误
1 parent e0ccbdd commit ef43d0d

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.frankdevhub.foo.chp2;
2+
3+
import java.util.concurrent.atomic.AtomicLong;
4+
5+
/**
6+
* @ClassName: Chp_2_3_6_AtomicIntegerService
7+
* @author: [email protected]
8+
* @date: 2019年11月16日 下午5:57:02
9+
* @description: 原子类也并不完全安全:方法与方法之间的调用有可能顺序错误
10+
* @Copyright: 2019 www.frankdevhub.site Inc. All rights reserved.
11+
*/
12+
13+
public class Chp_2_3_6_AtomicIntegerService {
14+
// 此处如果修改为
15+
// static AtomicLong aiRef = new AtomicLong(0L);
16+
// 可以保持原子性和方法调用均保持有序输出
17+
18+
public static AtomicLong aiRef = new AtomicLong(0L);
19+
20+
public long getValue() {
21+
return aiRef.get();
22+
}
23+
24+
synchronized public void addNum() {
25+
System.out.println(
26+
"threadName=" + Thread.currentThread().getName()
27+
+ " add 100 return value=" + aiRef.addAndGet(100));
28+
29+
aiRef.addAndGet(1);
30+
}
31+
}

Concurrency Programming Core/com-frankdevhub-multi-thread/src/com/frankdevhub/foo/chp2/Chp_2_3_6_AtomicIntegerUnSafeTest.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,10 @@
1212

1313
public class Chp_2_3_6_AtomicIntegerUnSafeTest {
1414

15-
protected class MyService {
16-
// 此处如果修改为
17-
// static AtomicLong aiRef = new AtomicLong(0L);
18-
// 可以保持原子性和方法调用均保持有序输出
19-
20-
private AtomicLong aiRef = new AtomicLong(0L);
21-
22-
public long getValue() {
23-
return aiRef.get();
24-
}
25-
26-
synchronized public void addNum() {
27-
System.out.println("threadName="+Thread.currentThread().getName()
28-
+ " add 100 return value=" + aiRef.addAndGet(100));
29-
30-
aiRef.addAndGet(1);
31-
}
32-
}
33-
3415
protected class MyThread extends Thread {
35-
private MyService service;
16+
private Chp_2_3_6_AtomicIntegerService service;
3617

37-
public MyThread(MyService service) {
18+
public MyThread(Chp_2_3_6_AtomicIntegerService service) {
3819
super();
3920
this.service = service;
4021
}
@@ -44,24 +25,25 @@ public void run() {
4425
service.addNum();
4526
}
4627
}
47-
28+
4829
// threadName=Thread-2 add 100 return value=200
4930
// threadName=Thread-4 add 100 return value=500
5031
// threadName=Thread-3 add 100 return value=300
5132
// threadName=Thread-0 add 100 return value=100
5233
// threadName=Thread-1 add 100 return value=400
5334
// last value print in main=505
54-
35+
5536
public static void main(String[] args) {
5637
try {
5738
Chp_2_3_6_AtomicIntegerUnSafeTest test = new Chp_2_3_6_AtomicIntegerUnSafeTest();
58-
MyService service = test.new MyService();
39+
Chp_2_3_6_AtomicIntegerService service = new Chp_2_3_6_AtomicIntegerService();
5940
Thread[] threads = new Thread[5];
60-
61-
for(int i=0;i<threads.length;i++){
62-
threads[i] = test.new MyThread(service);
41+
42+
for (int i = 0; i < threads.length; i++) {
43+
threads[i] = test.new MyThread(service);
44+
threads[i].setName("thread-[" + (i + 1) + "]");
6345
}
64-
46+
6547
for (int j = 0; j < threads.length; j++) {
6648
threads[j].start();
6749
}
@@ -72,7 +54,7 @@ public static void main(String[] args) {
7254
} catch (InterruptedException e) {
7355
e.printStackTrace();
7456
}
75-
57+
7658
}
7759

7860
}

0 commit comments

Comments
 (0)