-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclicBarrierExample.java
More file actions
39 lines (36 loc) · 1.52 KB
/
CyclicBarrierExample.java
File metadata and controls
39 lines (36 loc) · 1.52 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
34
35
36
37
38
39
import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class CyclicBarrierExample implements Runnable {
private static final int NUMBER_OF_THREADS = 5;
private static AtomicInteger counter = new AtomicInteger();
private static Random random = new Random(System.currentTimeMillis());
private static final CyclicBarrier barrier = new CyclicBarrier(5, new Runnable() {
public void run() {
counter.incrementAndGet();
}
});
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
for (int i = 0; i < NUMBER_OF_THREADS; i++) {
executorService.execute(new CyclicBarrierExample());
}
executorService.shutdown();
}
public void run() {
try {
while(counter.get() < 3) {
int randomSleepTime = random.nextInt(10000);
System.out.println("[" + Thread.currentThread().getName() + "] Sleeping for " + randomSleepTime);
Thread.sleep(randomSleepTime);
System.out.println("[" + Thread.currentThread().getName() + "] Waiting for barrier.");
barrier.await();
System.out.println("[" + Thread.currentThread().getName() + "] Finished.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}