-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
40 lines (37 loc) · 1.07 KB
/
Main.java
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
34
35
36
37
38
39
40
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
/**
* User: gkislin
* Date: 30.06.2014
*/
public class Main {
static final Object LOCK = new Object();
static final AtomicInteger ATOMIC_SUM = new AtomicInteger();
static final CountDownLatch CDL = new CountDownLatch(100000);
static int sum = 0;
volatile int i;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 100000; i++) {
new Thread() {
@Override
public void run() {
inc();
ATOMIC_SUM.incrementAndGet();
CDL.countDown();
}
}.start();
}
System.out.println(sum);
System.out.println(ATOMIC_SUM.get());
System.out.println("wait");
// Thread.sleep(500);
CDL.await();
System.out.println(sum);
System.out.println(ATOMIC_SUM.get());
}
static void inc() {
synchronized (LOCK) {
sum++;
}
}
}