Tutorials : Using the Queue Collection Effectively :

Using the Queue Collection Effectively

by Thribhuvan Thakur

Java's memory model guarantees that memory operations occur in a predictable order. For example, writes to volatile fields happen before any subsequent reads of those same fields—irrespective of any instructions to reorder through compiler optimization or JIT processing.

This guarantee is critical to java.util.concurrent's implementation, which uses reads and writes of volatile variables for atomic operations, thus providing the required synchronization.

In a concurrent queue, enqueuing operations happen before dequeuing, either from the same thread or among all threads, as stated in following lines from the concurrent API:

"Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a BlockingDeque happen before actions subsequent to the access or removal of that element from the BlockingDeque in another thread."

Luckily, the generalized Collection API and Queue specialization have gained functionality in the Java 1.5 and 1.6 JDKs. This article explores ways to characterize and categorize Queue collections for their performance, as well as how to use them in multi-threaded environments.

Iterating Collections

The standard way to traverse a collection is to use explicit iterators provided by the individual collection. It's also possible for one thread to modify a collection while iteration is in progress. This is done by using another thread and is known as 'concurrent modification'. You can also modify a collection directly while iterating through the same thread—for example, by calling collection.remove.

In this article, you'll be dealing with two types of iterators:

  1. Fail-fast: This type of iterator detects concurrent modifications to the underlying collection and fails quickly by throwing ConcurrentModificationException.
  2. Weakly consistent: This type of iterator does not detect concurrent modifications and never throws ConcurrentModificationException.
For example, the iterator for PriorityBlockingQueue creates a copy of the collection and traverses through the copy instead of the actual collection.

Author's Note: Fail-fast iterators are NOT designed to be fool proof. They catch concurrency errors on a best-effort basis, so use these iterators for debugging purposes only and consider using explicit locking (synchronized externally) when you want to iterate through a shared collection. .

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.