// Create a bounded blocking queue of integers
final int capacity = 10;
BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);

// Create a set of worker threads
final int numWorkers = 2;
Worker[] workers = new Worker[numWorkers];
for (int i=0; i<workers.length; i++) {
    workers[i] = new Worker(queue);

try {
    // Add some work to the queue; block if the queue is full.
    // Note that null cannot be added to a blocking queue.
    for (int i=0; i<100; i++) {

    // Add special end-of-stream markers to terminate the workers
    for (int i=0; i<workers.length; i++) {
} catch (InterruptedException e) {

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.