forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT7.java
More file actions
48 lines (36 loc) · 1 KB
/
T7.java
File metadata and controls
48 lines (36 loc) · 1 KB
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
package com.basic;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @program JavaBooks
* @description: volatile并不能保证的多线程的一致性
* @author: mf
* @create: 2019/12/28 23:46
*/
public class T7 {
AtomicInteger count = new AtomicInteger(0);
// /*volatile*/ int count = 0;
/*synchronized*/ void m() {
for (int i = 0; i < 1000; i++) {
// count++;
count.incrementAndGet();
}
}
public static void main(String[] args) {
T7 t7 = new T7();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 10; i++) {
threads.add(new Thread(t7::m, "thread-" + i));
}
threads.forEach((o) -> o.start());
threads.forEach((o) -> {
try {
o.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println(t7.count);
}
}