Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Condition — waiting for lock-protected state

Condition lets a thread wait for a change to shared state protected by a Lock. Learn await, awaitNanos, signal, and signalAll, and why the state check belongs in a loop.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

Condition changed = lock.newCondition();
changed.await();
long remaining = changed.awaitNanos(nanosTimeout);
changed.signal();
changed.signalAll();

Condition represents a wait queue associated with a Lock, not a boolean value. Create it through lock.newCondition(). One lock can have several such queues, for example for an empty buffer and a full buffer.

Condition.await atomically releases the lock while waiting and reacquires it before returning. Check the state in while, because spurious wakeups are possible. Condition.awaitNanos accepts nanoseconds and returns an estimated remaining time; pass that into the next wait rather than restarting the timeout. A result <= 0 means no time remains. Both methods can throw InterruptedException, clearing the interrupt status; propagate the exception or restore the status if you catch it.

Condition.signal wakes one waiting thread, whereas Condition.signalAll wakes all of them. Signalling does not transfer the lock: an awakened thread must reacquire it. For ReentrantLock, the calling thread must hold the lock when invoking a wait or signal method. Calling signal() or signalAll() without holding the lock throws IllegalMonitorStateException. In JDK 21, ReentrantLock's await() and awaitNanos() check the interrupt status before checking lock ownership. If the interrupt status is already set on entry, they throw InterruptedException even if the caller does not hold the lock. If the status is not set, calling without the lock throws IllegalMonitorStateException. Always release the lock in finally.

Check and modify the protected state under the same lock. The example prints ready whether or not publish runs before waiting begins: the ready state is retained, not the signal. This entry covers Java SE 21; neither the repository nor production runtime has been attested.

Examples

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionDemo {
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition changed = lock.newCondition();
    private boolean ready;

    void awaitReady() throws InterruptedException {
        lock.lock();
        try {
            while (!ready) {
                changed.await();
            }
        } finally {
            lock.unlock();
        }
    }

    void publish() {
        lock.lock();
        try {
            ready = true;
            changed.signalAll();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ConditionDemo demo = new ConditionDemo();
        Thread producer = new Thread(demo::publish);
        producer.start();
        demo.awaitReady();
        producer.join();
        System.out.println("ready");
    }
}

Result

ready

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources