dengxinyi 6 years ago
parent
commit
bb93c06f79

+ 39 - 0
basis/concurrent/TestBlockingQueue.java

@@ -0,0 +1,39 @@
+import java.util.concurrent.*;
+
+public class TestBlockingQueue {
+    public static void main(String[] args) {
+        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
+        ExecutorService executor = Executors.newCachedThreadPool();
+        for (int i = 0; i < 2; i++) {
+            executor.execute(() -> {
+                try {
+                    String str = queue.take();
+                    System.out.println("Take " + str);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            });
+        }
+        for (int i = 0; i < 3; i++) {
+            final int ms = i * 200;
+            executor.execute(() -> {
+                try {
+                    Thread.sleep(ms);
+                    queue.put("product");
+                    System.out.println("Put product");
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            });
+        }
+        executor.execute(() -> {
+            try {
+                String str = queue.take();
+                System.out.println("Take " + str);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        });
+        executor.shutdown();
+    }
+}

+ 6 - 0
basis/concurrent/TestCyclicBarrier.java

@@ -14,6 +14,12 @@ public class TestCyclicBarrier {
                 } catch (Exception e) {
                 } catch (Exception e) {
                     e.printStackTrace();
                     e.printStackTrace();
                 }
                 }
+                System.out.println("Thread " + num + " running.");
+                try {
+                    cb.await();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
                 System.out.println("Thread " + num + " end.");
                 System.out.println("Thread " + num + " end.");
             });
             });
         }
         }

+ 37 - 0
basis/concurrent/TestForkjoin.java

@@ -0,0 +1,37 @@
+import java.util.concurrent.*;
+
+public class TestForkjoin {
+    public static void main(String[] args) throws Exception {
+        MyRecursiveTask fj = new MyRecursiveTask(1, 100000);
+        ForkJoinPool pool = new ForkJoinPool();
+        Future future = pool.submit(fj);
+        System.out.println(future.get());
+    }
+}
+
+class MyRecursiveTask extends RecursiveTask<Integer> {
+    private final int threshold = 5;
+    private int beg;
+    private int end;
+
+    MyRecursiveTask(int beg, int end) {
+        this.beg = beg;
+        this.end = end;
+    }
+
+    @Override
+    protected Integer compute() {
+        if (end - beg <= threshold) {
+            int count = 0;
+            for (int i = beg; i <= end; i++)
+                count++;
+            return count;
+        }
+        int mid = beg + (end - beg) / 2;
+        MyRecursiveTask left = new MyRecursiveTask(beg, mid);
+        MyRecursiveTask right = new MyRecursiveTask(mid + 1, end);
+        left.fork();
+        right.fork();
+        return left.join() + right.join();
+    }
+}

+ 24 - 0
basis/concurrent/TestSemaphore.java

@@ -0,0 +1,24 @@
+import java.util.concurrent.*;
+
+public class TestSemaphore {
+    public static void main(String[] args) {
+        int resources = 2;
+        Semaphore semaphore = new Semaphore(resources);
+        ExecutorService executor = Executors.newCachedThreadPool();
+        for (int i = 1; i <= 10; i++) {
+            final int num = i;
+            executor.execute(() -> {
+                try {
+                    semaphore.acquire();
+                    System.out.println("Thread " + num + " start.");
+                    Thread.sleep(100);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                } finally {
+                    semaphore.release();
+                }
+            });
+        }
+        executor.shutdown();
+    }
+}