ReentrantLock Condition Bounded Buffer

Coordinate producer and consumer threads with one ReentrantLock and two Conditions so put blocks when the buffer is full and take blocks when empty.

JavaResilience

Code

snippet
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class BoundedBuffer {

    private final Queue<String> items = new ArrayDeque<>();
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition notFull = lock.newCondition();
    private final Condition notEmpty = lock.newCondition();
    private final int capacity;

    BoundedBuffer(int capacity) {
        this.capacity = capacity;
    }

    void put(String item) throws InterruptedException {
        lock.lock();
        try {
            while (items.size() == capacity) notFull.await(); // 已满:挂起生产者等腾位
            items.add(item);
            notEmpty.signal(); // 新元素到达:唤醒挂起的消费者
        } finally {
            lock.unlock();
        }
    }

    String take() throws InterruptedException {
        lock.lock();
        try {
            while (items.isEmpty()) notEmpty.await(); // 为空:挂起消费者等新元素
            String item = items.poll();
            notFull.signal();
            return item;
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        BoundedBuffer buffer = new BoundedBuffer(2);
        Thread producer = new Thread(() -> {
            try {
                for (int i = 1; i <= 3; i++) {
                    buffer.put("item-" + i);
                    System.out.println("produced item-" + i);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
        producer.start();
        Thread.sleep(50); // 先让缓冲填满:第三个 put 会阻塞在 notFull
        System.out.println("taken: " + buffer.take());
        System.out.println("taken: " + buffer.take());
        producer.join();
    }
}

How the snippet works

ReentrantLock Condition Bounded Buffer is built around the class BoundedBuffer.

while (items.size() == capacity) notFull.await(); // 已满:挂起生产者等腾位 is the loop that carries the per-item work.

Applying it

The demo output is labeled (produced item-, taken:), so every printed line maps back to its originating operation.

Statements that matter

Thread.sleep(50); // 先让缓冲填满:第三个 put 会阻塞在 notFull
System.out.println("taken: " + buffer.take());
Thread producer = new Thread(() -> {
    try {
        for (int i = 1; i <= 3; i++) {
            buffer.put("item-" + i);
            System.out.println("produced item-" + i);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
});

Interactive Tools for This Topic

FAQ

What does ReentrantLock Condition Bounded Buffer print when run?
The script runs standalone; its 2 labeled outputs include `produced item-`, `taken:`.