-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtomicIntegerTest.java
68 lines (60 loc) · 1.49 KB
/
AtomicIntegerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package java_thread;
import java.util.concurrent.atomic.AtomicInteger;
/** 测试类 */
public class AtomicIntegerTest {
public static void main(String[] args) {
AutomicOperation ao = new AutomicOperation();
//原子操作
ao.atomicOperation();
//非原子操作
ao.nonAtomicOperation();
}
}
/**
* 原子操作与非原子操作对比
*
* @author 唐龙
*/
class AutomicOperation{
private static final int THREAD_NUMBER=100;
static Thread[] ts = new Thread[THREAD_NUMBER];
static Integer nonValue = 0;
AtomicInteger atomicValue =new AtomicInteger();
/**非原子操作*/
public void nonAtomicOperation(){
threadsCreate('0');
System.out.printf("%d个线程进行加1操作后,数值变成:%d%n", THREAD_NUMBER,nonValue);
}
/**原子操作*/
public void atomicOperation(){
threadsCreate('1');
System.out.printf("%d个线程进行加1操作后,数值变成:%d%n", THREAD_NUMBER,atomicValue.intValue());
}
/**@param ch ‘0’:非原子操作,‘1’:原子操作*/
private void threadsCreate(char ch){
for (int i = 0; i < THREAD_NUMBER; i++) {
Thread t = new Thread(){
@Override
public void run(){
if('1' == ch) {
atomicValue.incrementAndGet();
}else{
nonValue++;
//nonValue进行同步,效果与原子操作相同
//synchronized(nonValue){nonValue++;}
}
}
};
t.start();
ts[i] = t;
}
//等待这些线程全部结束
for (Thread t : ts) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}