forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtomicLongEx2.java
More file actions
33 lines (21 loc) · 822 Bytes
/
AtomicLongEx2.java
File metadata and controls
33 lines (21 loc) · 822 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
33
package com.zetcode;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
public class AtomicLongEx2 {
private static AtomicLong sum = new AtomicLong();
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 5; i++) {
sum.set(0);
ExecutorService executorService = Executors.newFixedThreadPool(50);
for (int j = 1; j <= 50; j++) {
int finalI = i;
executorService.execute(() -> sum.addAndGet(finalI));
}
executorService.shutdown();
executorService.awaitTermination(3, TimeUnit.SECONDS);
System.out.println(sum);
}
}
}