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
|
final AtomicInteger ai = new AtomicInteger(0);
ai.getAndIncrement();
ai.getAndSet(3);
ai.getAndAdd(3);
int i = ai.compareAndExchange(0, 1);
boolean b = ai.compareAndSet(0, 1);
final AtomicIntegerArray aia = new AtomicIntegerArray(new int[]{1, 2, 3, 4, 5, 6});
int e = aia.get(0); // 1
int e1 = aia.getAndSet(0, 2); // 1
int e2 = aia.getAndIncrement(0); // 2
int e3 = aia.getAndAdd(0, 5); // 3
aia.compareAndSet(0, 8, 99); // [99, 2, 3, 4, 5, 6]
AtomicReference<User> ar = new AtomicReference<>();
User user = new User();
User user2 = new User();
ar.set(user);
ar.compareAndSet(user, user2);
AtomicIntegerFieldUpdater<User> aifu = AtomicIntegerFieldUpdater.newUpdater(User.class, "age");
User user1 = new User();
user1.setAge(22);
System.out.println(aifu.getAndIncrement(user)); // 22
System.out.println(aifu.get(user)); // 23
final String ref = "A";
final boolean mark = false;
final AtomicMarkableReference<String> amr = new AtomicMarkableReference<>(ref, mark);
amr.set("B", true); // 重新设置当前值和 mark 值
amr.compareAndSet(ref, "B", mark, true);
// 获取当前的ref和mark, 当前ref直接返回,当前mark存入mark[0]
boolean[] marks = new boolean[1];
String ref1 = amr.get(marks);
boolean mark1 = marks[0];
amr.attemptMark(ref, true); // 尝试设置mark值
final int stamp = 0;
final AtomicStampedReference<String> asr = new AtomicStampedReference<>(ref, stamp);
asr.set("B", 1); // 重新设置当前值和 stamp 值
asr.compareAndSet(ref, "B", 0, 1);
// 获取当前的ref和stamp, 当前ref直接返回,当前stamp存入stamp[0]
int[] stamps = new int[1];
String ref2 = asr.get(stamps);
int stamp2 = stamps[0];
asr.attemptStamp(ref, 88); // 尝试设置stamp值
|