Advertisement
DulcetAirman

Concurrency: notify vs signal (2)

Mar 12th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.foo;
  2.  
  3. import java.util.concurrent.locks.Condition;
  4. import java.util.concurrent.locks.Lock;
  5. import java.util.concurrent.locks.ReentrantLock;
  6.  
  7. public class LockingDemo {
  8.  
  9. /* What I get:
  10.  
  11. TEST 1: notify/wait
  12. Thread notifyB will wait...
  13. Thread notifyA notifies all.
  14. Thread notifyA: code after notifyAll()
  15. ... Thread notifyB is awaken.
  16. Thread notifyB: code after wait()
  17. TEST 2: signal/await
  18. Thread signalB will await...
  19. Thread signalA signals all.
  20. Thread signalA: code after signalAll()
  21. ... Thread signalB is awaken.
  22. Thread signalB: code after await()
  23. End of Test.
  24.  
  25. */
  26.   public static final void sleep() {
  27.     try {
  28.       Thread.sleep(500);
  29.     } catch (InterruptedException e) {
  30.       System.err.println("Interrupted!");
  31.       Thread.currentThread().interrupt();
  32.     }
  33.   }
  34.  
  35.   public static void main(String[] args) throws InterruptedException {
  36.  
  37.     final Lock monitor = new ReentrantLock(true);
  38.     final Condition cond1 = monitor.newCondition();
  39.  
  40.     final Thread notifyA = new Thread(new Runnable() {
  41.  
  42.       public void run() {
  43.         sleep();
  44.         synchronized (monitor) {
  45.           System.out.println("Thread notifyA notifies all.");
  46.           monitor.notifyAll();
  47.           sleep();
  48.           System.out.println("Thread notifyA: code after notifyAll()");
  49.         }//now notifyB can really be awaken.
  50.       }
  51.     });
  52.  
  53.     final Thread notifyB = new Thread(new Runnable() {
  54.  
  55.       public void run() {
  56.         synchronized (monitor) {
  57.           try {
  58.             System.out.println("Thread notifyB will wait...");
  59.             monitor.wait();
  60.             System.out.println("... Thread notifyB is awaken.");
  61.             sleep();
  62.             System.out.println("Thread notifyB: code after wait()");
  63.           } catch (InterruptedException e) {
  64.             e.printStackTrace();
  65.             Thread.currentThread().interrupt();
  66.           }
  67.         }
  68.       }
  69.  
  70.     });
  71.     System.out.println("TEST 1: notify/wait");
  72.     notifyB.start();
  73.     notifyA.start();
  74.     notifyB.join();
  75.     notifyA.join();
  76.     sleep();
  77.     System.out.println("TEST 2: signal/await");
  78.  
  79.     final Thread signalA = new Thread(new Runnable() {
  80.  
  81.       public void run() {
  82.         sleep();
  83.         monitor.lock();
  84.         try {
  85.           System.out.println("Thread signalA signals all.");
  86.           cond1.signalAll();
  87.           sleep();
  88.           System.out.println("Thread signalA: code after signalAll()");
  89.         } finally {
  90.           monitor.unlock(); //now signalB can really be awaken.
  91.         }
  92.       }
  93.     });
  94.  
  95.     final Thread signalB = new Thread(new Runnable() {
  96.  
  97.       public void run() {
  98.  
  99.         monitor.lock();
  100.           try {
  101.             System.out.println("Thread signalB will await...");
  102.             cond1.await();
  103.             System.out.println("... Thread signalB is awaken.");
  104.             sleep();
  105.             System.out.println("Thread signalB: code after await()");
  106.           } catch (InterruptedException e) {
  107.             e.printStackTrace();
  108.             Thread.currentThread().interrupt();
  109.           } finally {
  110.             monitor.unlock();
  111.           }
  112.         }
  113.  
  114.     });
  115.  
  116.     signalB.start();
  117.     signalA.start();
  118.     signalB.join();
  119.     signalA.join();
  120.     sleep();
  121.  
  122.     System.out.println("End of Test.");
  123.   }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement