import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; class BoundedBuffer { private BlockingQueue buffer; public BoundedBuffer(int capacity) { buffer = new ArrayBlockingQueue<>(capacity); } public void produce(int value) throws InterruptedException { buffer.put(value); System.out.println("Produced: " + value); } public int consume() throws InterruptedException { int value = buffer.take(); System.out.println("Consumed: " + value); return value; } } class Producer implements Runnable { private BoundedBuffer buffer; public Producer(BoundedBuffer buffer) { this.buffer = buffer; } public void run() { try { for (int i = 0; i < 10; i++) { buffer.produce(i); Thread.sleep(100); // Simulating production time } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } class Consumer implements Runnable { private BoundedBuffer buffer; public Consumer(BoundedBuffer buffer) { this.buffer = buffer; } public void run() { try { for (int i = 0; i < 10; i++) { buffer.consume(); Thread.sleep(200); // Simulating consumption time } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } public class Main { public static void main(String[] args) { BoundedBuffer buffer = new BoundedBuffer(5); Thread producerThread = new Thread(new Producer(buffer)); Thread consumerThread = new Thread(new Consumer(buffer)); producerThread.start(); consumerThread.start(); } }