Skip to content

Commit 463a55e

Browse files
committed
Atomic update
1 parent e98f201 commit 463a55e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/com/winterbe/java8/samples/concurrent/Atomic1.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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() {

src/com/winterbe/java8/samples/concurrent/ConcurrentUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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");

0 commit comments

Comments
 (0)