Lock & Condition Example..

..
package biz.tugay.sandbox;
 
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 14/07/2017
 */
public class LockAndCondition {
 
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();
 
    // only completes if bar has completed for this instance!
    void foo() {
        lock.lock();
        System.out.println("foo acquired lock!");
 
        try {
            System.out.println("foo is now waiting for a signal from condition!");
            condition.await();
        } catch (InterruptedException ignored) {
            // ignored
        }
 
        System.out.println("foo woke up, will release lock now!");
        lock.unlock();
    }
 
    void bar() {
        lock.lock();
        System.out.println("bar acquired lock!");
        System.out.println("bar will signalAll now!");
        condition.signalAll();
        System.out.println("bar signaled all, will release lock now!");
        lock.unlock();
    }
}
package biz.tugay.sandbox;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 14/07/2017
 */
public class App {
 
    public static void main(String[] args) throws InterruptedException {
        final LockAndCondition lockAndCondition = new LockAndCondition();
 
        final Runnable fooRunnable = new Runnable() {
            @Override
            public void run() {
                lockAndCondition.foo();
            }
        };
 
        final Runnable barRunnable = new Runnable() {
            @Override
            public void run() {
                lockAndCondition.bar();
            }
        };
 
        final Thread fooThread = new Thread(fooRunnable);
        final Thread barThread = new Thread(barRunnable);
 
        fooThread.start();
        Thread.sleep(2000);
        barThread.start();
 
        fooThread.join();
        barThread.join();
    }
}

And the output will be..
foo acquired lock!
foo is now waiting for a signal from condition!
bar acquired lock!
bar will signalAll now!
bar signaled all, will release lock now!
foo woke up, will release lock now!

Things to Remember

  • The java.util.concurrent.locks.Condition interface is the modern replacement for the wait and notify methods.
  • Before calling await, you must have locked the Lock used to produce the Condition.
  • It is possible that the awaiting thread may be interrupted and you must handle the possible InterruptedException.
  • When you call the await method, the Lock associated with the Condition is released. 
  • Before the await method returns, the lock will be reacquired. 
  • In order to use a Condition, a thread must first acquire a Lock.