Advertisement
DulcetAirman

concurrency: notify vs signal

Mar 11th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. //---------------------------------
  2. Object lock = new Object();
  3. //Thread A:
  4. synchronized(lock) {
  5.  lock.notifyAll();
  6.  runBeforeThreadBisNotified();
  7. }
  8. //-----
  9. //Thread B:
  10. synchronized(lock) {
  11.  lock.wait();
  12.  runOnNotificationFromThreadA();
  13. }
  14. //---------------------------------
  15.  
  16.  final Lock lock = new ReentrantLock();
  17.  final Condition cond = lock.newCondition();
  18. //Thread A:
  19.  lock.lock();
  20.  try {
  21.   cond.signal();
  22.   runWhenThreadBisFinished() ;
  23.  } finally {
  24.  lock.unlock();
  25.  }
  26. //-----
  27. //Thread B:
  28.  lock.lock();
  29.  try {
  30.   cond.await();
  31.   runWhenSignaled() ;
  32.  } finally {
  33.  lock.unlock();
  34.  }
  35. //---------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement