-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtomicTest.java
More file actions
49 lines (46 loc) · 1.44 KB
/
Copy pathAtomicTest.java
File metadata and controls
49 lines (46 loc) · 1.44 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
49
package ThreadTest;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
/*
* 原子类 使得java语言中的有些非原子性操作变成原子性的,不会出现线程竞争的情况
* 原子性操作在java语言中,主要有基本数据类型的赋值和返回操作
*
* */
public class AtomicTest implements Runnable {
private AtomicInteger i=new AtomicInteger(0);
@Override
public void run() {
while (true){
}
}
public void add(){
i.addAndGet(2);
}
public int getI() {
return i.get();
}
public static void main(String[] args) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
System.out.print("Aborting");
System.exit(0);
}
},5000); //Timer 相当于一个计时器,5000ms后程序结束
ExecutorService executorService=Executors.newCachedThreadPool();
AtomicTest atomicTest=new AtomicTest();
executorService.execute(atomicTest);
while (true){
int val=atomicTest.getI();
if (val%2!=0){
System.out.print(val);
System.exit(0);
}
}
}
}