dengxinyi hace 6 años
padre
commit
a1bab67af2
Se han modificado 2 ficheros con 107 adiciones y 0 borrados
  1. 64 0
      basis/concurrent/TestAwaitAndSignal.java
  2. 43 0
      basis/concurrent/TestWaitAndNotify.java

+ 64 - 0
basis/concurrent/TestAwaitAndSignal.java

@@ -0,0 +1,64 @@
+import java.util.concurrent.locks.*;
+
+public class TestAwaitAndSignal {
+    public static void main(String[] args) {
+        Lock lock = new ReentrantLock();
+        Condition c1 = lock.newCondition();
+        Condition c2 = lock.newCondition();
+        Condition c3 = lock.newCondition();
+        AwaitThread t1 = new AwaitThread(lock, c1, c2, 1, 3, 50, true);
+        AwaitThread t2 = new AwaitThread(lock, c2, c3, 2, 3, 50, false);
+        AwaitThread t3 = new AwaitThread(lock, c3, c1, 3, 3, 50, false);
+        t1.start();
+        t2.start();
+        t3.start();
+        try {
+            Thread.sleep(60);
+            lock.lock();
+            c1.signal();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            lock.unlock();
+        }
+    }
+}
+
+class AwaitThread extends Thread {
+    Lock lock;
+    Condition curr;
+    Condition next;
+    int init;
+    int delta;
+    int count;
+    boolean first;
+
+    AwaitThread(Lock lock, Condition curr, Condition next, int init, int delta, int count, boolean first) {
+        this.lock = lock;
+        this.curr = curr;
+        this.next = next;
+        this.init = init;
+        this.delta = delta;
+        this.count = count;
+        this.first = first;
+    }
+
+    @Override
+    public void run() {
+        System.out.println("Thread " + init + " is running.");
+        for (int i = 0, num = init; i < count; i++) {
+            lock.lock();
+            try {
+                curr.await();
+                System.out.println(num);
+                num += delta;
+                Thread.sleep(5);
+                next.signal();
+            } catch (Exception e) {
+                e.printStackTrace();
+            } finally {
+                lock.unlock();
+            }
+        }
+    }
+}

+ 43 - 0
basis/concurrent/TestWaitAndNotify.java

@@ -0,0 +1,43 @@
+public class TestWaitAndNotify {
+    public static void main(String[] args) {
+        WaitThread t1 = new WaitThread(1, 2, 50, false);
+        WaitThread t2 = new WaitThread(2, 2, 50, false);
+        t1.start();
+        t2.start();
+    }
+}
+
+class MyLock {}
+
+class WaitThread extends Thread {
+    private static MyLock lock = new MyLock();
+    private int init;
+    private int delta;
+    private int count;
+    private boolean isLast;
+
+    WaitThread(int init, int delta, int count, boolean isLast) {
+        this.init = init;
+        this.delta = delta;
+        this.count = count;
+        this.isLast = isLast;
+    }
+
+    @Override
+    public void run() {
+        for (int i = 0, num = init; i < count; i++) {
+            System.out.println(num);
+            num += delta;
+            if (isLast && i == count - 1) return;
+            synchronized (lock) {
+                lock.notify();
+                if (!isLast && i == count - 1) return;
+                try {
+                    lock.wait();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+}