public class C12D1 {
public static volatile int race = 0;
public static void increase() {
race++;
}
private static final int THREADS_COUNT = 20;
public static void main(String[] args) {
CountDownLatch countDownLatch = new CountDownLatch(THREADS_COUNT);
Thread[] threads = new Thread[THREADS_COUNT];
for (int i = 0; i < THREADS_COUNT; i++) {
threads[i] = new Thread(() -> {
try {
for (int j = 0; j < 10000; j++) {
increase();
}
} finally {
countDownLatch.countDown();
}
});
threads[i].start();
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(race);
}
}
-------
36654



