Tame Tiger: Concurrent collections

Source: Internet
Author: User
Tags empty

In the early stages of Java programming, a professor at the State University of New York (SUNY) in Oswego decided to create a simple library to help developers build applications that would better handle multi-threaded situations. This is not to say that it is not possible to use an existing library, but as with a standard network library, it is easier to handle multithreading by using a debugged, trusted library. With the help of a Addision-wesley book, the library has become more and more popular. Finally, the author Doug Lea decided to try to make it a standard part of the Java platform--jsr-166. The library eventually became the Tiger version of the Java.util.concurrent package. In this new tame Tiger trick, we will explore the new Queue interface in the Collection Framework, the Concurrency and concurrency implementation of this interface, concurrent MAP implementations, and concurrent List and Set implementations that are much more than write operations.

Introducing the Queue interface

The Java.util package provides a new basic interface for the collection: Java.util.Queue. While it is certainly possible to add and remove Java.util.List as a queue at the opposite ends, the new queue interface provides additional ways to support adding, removing, and checking collections, as follows:

public boolean offer(Object element)
public Object remove()
public Object poll()
public Object element()
public Object peek()

Basically, a queue is a first in first out (FIFO) data structure. Some queues have size limits, so if you want to add a new item to a full queue, the extra items are rejected. Then the new offer method will work. It does not throw a unchecked exception to the call to the Add () method, but only the false returned by an offer (). The Remove () and poll () methods remove the first element (head) from the queue. The Remove () behavior is similar to the version of the Collection interface, but the new poll () method does not throw an exception when invoked with an empty collection, but returns NULL. Therefore, the new method is more suitable for situations that are prone to abnormal conditions. The latter two methods element () and peek () are used to query elements in the header of the queue. Similar to the Remove () method, Element () throws an exception when the queue is empty, and peek () returns NULL.

Using basic queues

There are two sets of Queue implementations in Tiger: The new Blockingqueue interface is implemented and the interface is not implemented. I will first analyze those that have not been implemented.

In the simplest case, the original Java.util.LinkedList implementation has been transformed into not only the implementation of Java.util.List interface, but also the implementation of Java.util.Queue interface. You can consider a set as either of these two. Listing 1 shows one way to use LinkedList as a Queue:

Listing 1. Using 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());

A little more complicated is the new Java.util.AbstractQueue class. This class works in the same way as Java.util.AbstractList and Java.util.AbstractSet classes. When you create a custom collection, you do not implement the entire interface yourself, but simply inherit the abstract implementation and fill in the details. When you use Abstractqueue, you must provide implementations for method offers (), poll (), and Peek (). Methods such as Add () and AddAll () are modified to use an offer (), while clear () and remove () use poll (). Finally, Element () uses peek (). Of course, you can provide an optimized implementation of these methods in subclasses, but it is not necessary to do so. And, instead of creating your own subclasses, you can use several built-in implementations, two of which are non-blocking queues: Priorityqueue and Concurrentlinkedqueue.

The Priorityqueue and Concurrentlinkedqueue classes add two specific set implementations to the Collection Framework. The Priorityqueue class essentially maintains a sequence table. Elements added to the Queue are positioned according to their natural ordering (through their java.util.Comparable implementation) or based on the Java.util.Comparator implementation passed to the constructor. Changing the LinkedList in Listing 2 to Priorityqueue will print out Four instead of one, because alphabetical--the natural order of the strings--four is the first. Concurrentlinkedqueue is a thread-safe queue based on a linked node. Concurrent access does not require synchronization. Because it adds elements to the tail of the queue and deletes them from the head, Concurrentlinkedqueue shared access to the public collection works well as long as the queue does not need to be known. Gathering information about the size of a queue is slow and requires traversing the queue.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.