|
@@ -0,0 +1,22 @@
|
|
|
+import java.util.concurrent.*;
|
|
|
+
|
|
|
+public class TestCyclicBarrier {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ int threads = 10;
|
|
|
+ CyclicBarrier cb = new CyclicBarrier(threads);
|
|
|
+ ExecutorService executor = Executors.newFixedThreadPool(threads);
|
|
|
+ for (int i = 0; i < threads; i++) {
|
|
|
+ final int num = i + 1;
|
|
|
+ executor.execute(() -> {
|
|
|
+ System.out.println("Thread " + num + " start.");
|
|
|
+ try {
|
|
|
+ cb.await();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ System.out.println("Thread " + num + " end.");
|
|
|
+ });
|
|
|
+ }
|
|
|
+ executor.shutdown();
|
|
|
+ }
|
|
|
+}
|