-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCasMainV2.java
More file actions
31 lines (24 loc) · 951 Bytes
/
Copy pathCasMainV2.java
File metadata and controls
31 lines (24 loc) · 951 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
package thread.cas;
import util.MyLogger;
import java.util.concurrent.atomic.AtomicInteger;
public class CasMainV2 {
public static void main(String[] args) {
AtomicInteger atomicInteger = new AtomicInteger();
System.out.println("start value = " + atomicInteger.get());
int resultValue1 = increamentAndGet(atomicInteger);
System.out.println("resultValue1 = " + resultValue1);
int resultValue2 = increamentAndGet(atomicInteger);
System.out.println("resultValue2 = " + resultValue2);
}
public static int increamentAndGet(AtomicInteger atomicInteger){
int getValue;
boolean result;
do{
getValue = atomicInteger.get();
MyLogger.log("getValue : " + getValue);
result = atomicInteger.compareAndSet(getValue, getValue+1);
MyLogger.log("result : " + result);
}while(!result);
return getValue + 1;
}
}