forked from Apress/functional-interfaces-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsafe.java
More file actions
37 lines (35 loc) · 1.04 KB
/
Unsafe.java
File metadata and controls
37 lines (35 loc) · 1.04 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
package chapter14;
import java.util.concurrent.*;
public class Unsafe
{
static int num = 0;
public static void main(String[] args)
{
CompletableFuture<Integer> cf1 =
CompletableFuture.supplyAsync( () -> {
for (int i=0; i < 10000; ++i)
{
int x = num;
num += 10;
if (num != (x+10))
System.out.println("cf1 THREAD READ ERROR: x= "
+ x +" num= " + num);
}
return num;
});
CompletableFuture<Integer> cf2 =
CompletableFuture.supplyAsync( () -> {
for (int i=0; i < 10000; ++i)
{
int x = num;
num -= 10;
if (num != (x-10))
System.out.println("cf2 THREAD READ ERROR: x= "
+ x +" num= " + num);
}
return num;
});
CompletableFuture.allOf(cf1, cf2)
.join();
}
}