-
Notifications
You must be signed in to change notification settings - Fork 3
/
TestAtomicDemo.java
55 lines (44 loc) · 1020 Bytes
/
TestAtomicDemo.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
package com.jucday01;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
//volatile i++的原子性问题
/*
* 原子变量(volatile保证内存可见性,CAS算法保证数据的原子性,Compare and Swap)
*/
public class TestAtomicDemo {
/*
@Test
public void test() {
int i=10;
i=i++;
System.out.println(i);
// int temp=i;
//i=i+1;
//i=temp;
}
*/
public static void main(String[] args) {
AtomicDemo ad=new AtomicDemo();
for(int i=0;i<10;i++) {
new Thread(ad).start();
}
}
}
class AtomicDemo implements Runnable{
// private volatile int serialNumber=0;
private AtomicInteger serialNumber=new AtomicInteger();
public int getSerialNumber() {
return serialNumber.getAndIncrement();
}
public void setSerialNumber(AtomicInteger serialNumber) {
this.serialNumber = serialNumber;
}
@Override
public void run() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {;
}
System.out.println(Thread.currentThread().getName()+":"+getSerialNumber());
}
}