dengxinyi 6 năm trước cách đây
mục cha
commit
72e734e8bf

+ 19 - 0
basis/concurrent/TestCountDownLatch.java

@@ -0,0 +1,19 @@
+import java.util.concurrent.*;
+
+public class TestCountDownLatch {
+    public static void main(String[] args) throws Exception {
+        int threads = 10;
+        CountDownLatch cd = new CountDownLatch(threads);
+        ExecutorService executor = Executors.newCachedThreadPool();
+        for (int i = 0; i < 10; i++) {
+            final int num = i + 1;
+            executor.execute(() -> {
+                System.out.println("Thread " + num + " start.");
+                cd.countDown();
+            });
+        }
+        cd.await();
+        System.out.println("All finished.");
+        executor.shutdown();
+    }
+}

+ 22 - 0
basis/concurrent/TestCyclicBarrier.java

@@ -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();
+    }
+}