File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed
src/com/winterbe/java8/samples/concurrent Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,24 @@ public class Atomic1 {
1818 public static void main (String [] args ) {
1919 testIncrement ();
2020 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 ());
2139 }
2240
2341 private static void testAccumulate () {
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ public class ConcurrentUtils {
1111 public static void stop (ExecutorService executor ) {
1212 try {
1313 executor .shutdown ();
14- executor .awaitTermination (5 , TimeUnit .SECONDS );
14+ executor .awaitTermination (60 , TimeUnit .SECONDS );
1515 }
1616 catch (InterruptedException e ) {
1717 System .err .println ("termination interrupted" );
You can’t perform that action at this time.
0 commit comments