Java
  Home arrow Java arrow Page 2 - Taming Tiger: Concurrent Collections
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Taming Tiger: Concurrent Collections
By: developerWorks
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 11
    2004-10-27

    Table of Contents:
  • Taming Tiger: Concurrent Collections
  • Using the Basic Queues
  • Using the Blocking Queues
  • Using the ConcurrentMap implementation
  • Resources

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    PCmover - $15 Off with Coupon Code CJPH7Q

    Taming Tiger: Concurrent Collections - Using the Basic Queues
    (Page 2 of 5 )

    There are two sets of Queue implementations in Tiger: those that implement the new BlockingQueue interface, and those that don't. First, I'll take a look at those that don't.

    In the simplest case, the pre-existing java.util.LinkedList implementation has been retrofitted to implement not only the java.util.List interface, but also the java.util.Queue interface. You can treat the collection as either. Listing 1 shows one way a LinkedList as a Queue can be used:

    Listing 1. Using a Queue implementation

    Queue queue = new LinkedList();
    queue.offer("One");
    queue.offer("Two");
    queue.offer("Three");
    queue.offer("Four");
    // Head of queue should be One
    System.out.println("Head of queue is: " + queue.poll());

    Next up in complexity is the new java.util.AbstractQueue class. This class works similarly to the java.util.AbstractList and java.util.AbstractSet classes. Instead of implementing the entire interface yourself when creating a custom collection, you just subclass the abstract implementation and fill in the details. With AbstractQueue, the methods you must provide implementations for are offer(), poll(), and peek(). Methods like add() and addAll() are modified to use offer(), while clear() and remove() use poll(). Finally, element() uses peek(). You certainly can provide optimized implementations of these methods in your subclasses, but you don't have to. And, instead of creating your own subclasses, you can use one of the several built-in implementations, two of which aren't blocking queues: PriorityQueue and ConcurrentLinkedQueue.

    The PriorityQueue and ConcurrentLinkedQueue classes add two more concrete collection implementations to the Collections Framework. The PriorityQueue class essentially maintains an ordered list. Elements added to a Queue are positioned in the PriorityQueue class based on either their natural ordering (through its java.util.Comparable implementation) or according to the java.util.Comparator implementation passed into the constructor. Changing LinkedList in Listing 2 to PriorityQueue will result in Four being printed out instead of One, as Four comes first alphabetically -- the natural ordering of strings. A ConcurrentLinkedQueue is a thread-safe queue based on linked nodes. Concurrent access does not need to be synchronized. Because it adds elements to the end of the queue and removes them from the head, ConcurrentLinkedQueue works well for shared access to a common collection, as long as you don't need to know the size of the queue. Collecting this information is slow, requiring queue traversal.

    IBM developerWorksVisit developerWorks for thousands of developer articles, tutorials, and resources related to open standard technologies, IBM products, and more. See developerWorks.

    More Java Articles
    More By developerWorks


     

       

    JAVA ARTICLES

    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections

    Iron Speed
     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway