Thursday, 8 January 2009

the joys of java.util.concurrent

Since java ONE-FIVE we have had the java.util.concurrent package, which introduced the remarkable BlockingQueue interface (which extends java.util.Queue). The "B&Q" interface has a wide range of implementations:
  • ArrayBlockingQueue
  • DelayQueue
  • LinkedBlockingQueue (a bq based on linked nodes)
  • PriorityBlockingQueue
  • SynchronousQueue

An ArrayBlockingQueue is a BOUNDED blocking queue backed by an array. It orders elements using FIFO discipline. This is the classic "bounded buffer" in which a fixed-size array holds elements inserted by producers and extracted by consumers. The head of the queue is the element that has been on the queue the longest time.

These data structures are all part of the wonderful Java Collections Framework. Enjoy the benefits of enhanced-for loop, generics and auto-boxing.

Here's a precanned example of enhanced-for-loop in action, utilising the colon operator:

void cancelAll(Collection c) { for (TimerTask t : c) t.cancel();}

No comments: