Multithreading: a Condition splits one lock into wait sets

Condition.await() and signal() work like wait()/notify() but on a Lock instead of an intrinsic monitor, and a single lock can create several independent conditions. The waiting thread only proceeds once the guarded flag is true and it has been signalled.

Code
ReentrantLock lock = new ReentrantLock();
Condition ready = lock.newCondition();
boolean[] flag = new boolean[1];
String[] result = new String[1];
Thread waiter = new Thread(() -> {
    lock.lock();
    try {
        while (!flag[0]) ready.await();
        result[0] = "proceeding";
    } catch (InterruptedException e) {
    } finally {
        lock.unlock();
    }
});
waiter.start();
Thread.sleep(20);
lock.lock();
try {
    flag[0] = true;
    ready.signal();
} finally {
    lock.unlock();
}
waiter.join();
System.out.println("Waiter result: " + result[0]);
Output
Waiter result: proceeding
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms