forked from Apress/functional-interfaces-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafe.java
More file actions
32 lines (28 loc) · 797 Bytes
/
Safe.java
File metadata and controls
32 lines (28 loc) · 797 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
32
package chapter14;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
public class Safe
{
static AtomicInteger num = new AtomicInteger(0);
public static void main(String[] args)
{
CompletableFuture<Integer> cf1 =
CompletableFuture.supplyAsync( () -> {
for (int i=0; i < 10000; ++i)
{
int x = num.addAndGet(10);
}
return num.get();
});
CompletableFuture<Integer> cf2 =
CompletableFuture.supplyAsync( () -> {
for (int i=0; i < 10000; ++i)
{
int x = num.addAndGet(-10);
}
return num.get();
});
CompletableFuture.allOf(cf1, cf2)
.join();
}
}