-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathThreadSynchronizationExample.java
More file actions
29 lines (23 loc) · 803 Bytes
/
Copy pathThreadSynchronizationExample.java
File metadata and controls
29 lines (23 loc) · 803 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
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadSynchronizationExample {
private static final int NUM_THREADS = 10;
public static void main(String[] args) {
AtomicInteger counter = new AtomicInteger(0);
for (int i = 0; i < NUM_THREADS; i++) {
Thread thread = new Thread(() -> {
for (int j = 0; j < 1000; j++) {
counter.incrementAndGet();
}
});
thread.start();
}
try {
for (Thread thread : Thread.getAllStackTraces().keySet()) {
thread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Final counter value: " + counter.get());
}
}