File tree 2 files changed +19
-1
lines changed
src/com/winterbe/java8/samples/concurrent 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,24 @@ public class Atomic1 {
18
18
public static void main (String [] args ) {
19
19
testIncrement ();
20
20
testAccumulate ();
21
+ testUpdate ();
22
+ }
23
+
24
+ private static void testUpdate () {
25
+ atomicInt .set (0 );
26
+
27
+ ExecutorService executor = Executors .newFixedThreadPool (2 );
28
+
29
+ IntStream .range (0 , NUM_INCREMENTS )
30
+ .forEach (i -> {
31
+ Runnable task = () ->
32
+ atomicInt .updateAndGet (n -> n + 2 );
33
+ executor .submit (task );
34
+ });
35
+
36
+ ConcurrentUtils .stop (executor );
37
+
38
+ System .out .format ("Update: %d\n " , atomicInt .get ());
21
39
}
22
40
23
41
private static void testAccumulate () {
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ public class ConcurrentUtils {
11
11
public static void stop (ExecutorService executor ) {
12
12
try {
13
13
executor .shutdown ();
14
- executor .awaitTermination (5 , TimeUnit .SECONDS );
14
+ executor .awaitTermination (60 , TimeUnit .SECONDS );
15
15
}
16
16
catch (InterruptedException e ) {
17
17
System .err .println ("termination interrupted" );
You can’t perform that action at this time.
0 commit comments