-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtomicIntegerDemo.java
More file actions
56 lines (41 loc) · 1.47 KB
/
AtomicIntegerDemo.java
File metadata and controls
56 lines (41 loc) · 1.47 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
50
51
52
53
54
55
56
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
public class AtomicIntegerDemo extends ConcurrentUtils {
public static void main(String[] args) {
/*
* the atomic classes make heavy use of compare-and-swap (CAS), an
* atomic instruction directly supported by most modern CPUs. Those
* instructions usually are much faster than synchronizing via locks. So
* prefer atomic classes over locks in case you just have to change a
* single mutable variable concurrently.
*/
AtomicInteger atomicInt = new AtomicInteger(0);
ExecutorService executor = Executors.newFixedThreadPool(2);
IntStream.range(0, 1000).forEach(
i -> executor.submit(atomicInt::incrementAndGet));
sleep(2);
System.out.println(atomicInt.get()); // => 1000
AtomicInteger atomicInt2 = new AtomicInteger(0);
IntStream.range(0, 1000).forEach(i ->
{
Runnable task = () -> atomicInt2.updateAndGet(n -> n + 2);
executor.submit(task);
});
sleep(2);
System.out.println(atomicInt2.get()); // => 2000
AtomicInteger atomicInt3 = new AtomicInteger(0);
IntStream.range(0, 4).forEach(
i ->
{
//m is i value, and n is current value
//1 + 2*2 + 3*3
Runnable task = () -> atomicInt3.accumulateAndGet(i, (
n, m) -> n + m*m);
executor.submit(task);
});
stop(executor);
System.out.println(atomicInt3.get());
}
}