forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
33 lines (23 loc) · 1.08 KB
/
Copy pathMain.java
File metadata and controls
33 lines (23 loc) · 1.08 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
package modern.challenge;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws InterruptedException {
Incrementator nonAtomicInc = new Incrementator();
AtomicIncrementator atomicInc = new AtomicIncrementator();
ExecutorService executor = Executors.newFixedThreadPool(5);
System.out.println("Counting via non atomic incrementator ...");
for (int i = 0; i < 1_000_000; i++) {
executor.execute(nonAtomicInc);
}
System.out.println("Counting via atomic incrementator ...");
for (int i = 0; i < 1_000_000; i++) {
executor.execute(atomicInc);
}
executor.shutdown();
executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
System.out.println("Non atomic counting result (expected 1_000_000): " + nonAtomicInc.getCount());
System.out.println("Atomic counting result (expected 1_000_000): " + atomicInc.getCount());
}
}