-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadAtomic.java
More file actions
37 lines (31 loc) · 857 Bytes
/
Copy pathThreadAtomic.java
File metadata and controls
37 lines (31 loc) · 857 Bytes
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
package secondPart;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author yangxp
* @date 2017年8月7日 下午4:44:58
* AtomicInteger i++ 的原子性
*/
public class ThreadAtomic {
public static void main(String[] args) {
ThreadAtomicTest threadAtomicTest = new ThreadAtomicTest();
Thread t1 = new Thread(threadAtomicTest);
t1.start();
Thread t2 = new Thread(threadAtomicTest);
t2.start();
Thread t3 = new Thread(threadAtomicTest);
t3.start();
Thread t4 = new Thread(threadAtomicTest);
t4.start();
Thread t5 = new Thread(threadAtomicTest);
t5.start();
}
}
class ThreadAtomicTest extends Thread {
private AtomicInteger count = new AtomicInteger(0);
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
System.out.println(count.incrementAndGet());
}
}
}