Home  Synchronization and concurrency  wait/notify  final  volatile  synchronized keyword  Java threading  Deadlock (and avoiding it)  Java 5: ConcurrentHashMap  Atomic variables  Explicit locks  Queues  Semaphores  CountDownLatch  CyclicBarrier

The producer-consumer pattern in Java 5: using blocking queues in preference to wait()/notify() (ctd)

On the previous page, we looked at using wait/notify to implement a producer-consumer pattern. As mentioned, some new Java 5 classes allow us to separate out the queue which holds pending tasks from the "real" functionality of the producer/consumer.

The BlockingQueue interface

The Java 5 concurrent package provides the BlockingQueue interface and various implementations. Blocking queues offer (among others) the following methods:

public void put(E o);
public E take();

This is a parametrised class: in other words, E, represents the type of object that we declare the queue to hold.

A common queue implementation is ArrayBlockingQueue, which has a fixed bound on the number of elements it can hold, or LinkedBlockingQueue, which need not have a limit (other than that of available memory). Using the latter, this is how our logging thread might look:

public class LoggingThread extends Thread {
  private BlockingQueue<String> linesToLog =
    new LinkedBlockingQueue<String>();

  public void run() {
    try {
      while (!terminateRequested) {
        String line = linesToLog.take();
        doLogLine(line);
      }
    } catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
    }
  }

  public void log(String line) {
    linesToLog.put(line);
  }
}

The code is now cleaner since the gubbinry of synchronization and notifying is taken care of by the blocking queue. In addition, it is now simple for us to modify the code to place different requirements on the queue.

More information on blocking queues

See the following pages:

Article written by Neil Coffey (@BitterCoffey).

Software

 LetterMeister (word puzzle game for iPhone)
 Currency Quoter (currency converter/predictor)
 French Vocab Games for iPhone/iPad
 Vocabularium: create Spanish vocab podcasts


Java programming articles and tutorials on this site are written by Neil Coffey (@BitterCoffey). Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles, or if you simply have a programming question that you would like to see answered on this site. Most topics will be considered. But in particular, the site aims to provide tutorials and information on topics that aren't well covered elsewhere, or on Java performance information that is poorly described or understood. Suggestions may be made via the Javamex blog (see the site's front page for details).
Copyright © Neil Coffey 2014. All rights reserved.