In layman's Java Concurrency (25): Concurrent container Part 10 bidirectional concurrent blocking queue blockingdeque[go]

Source: Internet
Author: User

This section describes the last tool of the queue and is the most powerful tool. You can see the feature of this tool from the name: bidirectional concurrent blocking queue. The so-called bidirectional means that can be manipulated from the head and tail of the queue, concurrency is just a thread-safe implementation, blocking allows the queue to be suspended when the queuing team does not meet the conditions, which is said to refer to a list of supported Fifo/filo implementation.

First look at the data structure of the linkedblockingdeque. Often, the pros and cons of this implementation can be seen from the data structure, so you know how to use the tool better.

The following conclusions can be drawn from the data structure and functional requirements:

    1. To support blocking, the capacity of the queue must be fixed, or the thread cannot be suspended while enqueued. That is, capacity is the final type.
    2. Since it is a doubly linked list, each node needs to be preceded by two references, so that all elements can be concatenated together to support bidirectional traversal. You also need to prev/next two references.
    3. Two-way linked list needs to operate at the same time, so need to first/last two nodes, of course, you can refer to LinkedList as a two-way node to complete, so that the implementation of a bit more trouble.
    4. Since the blocking feature is supported, locks and condition variables are required to suspend the thread. This is done using a lock of two conditional variables.

With the above conclusions, we will study the advantages and disadvantages of linkedblockingdeque.

The advantage, of course, is that the feature is powerful enough, and because of the use of an exclusive lock, it is easier to implement. All operations on the queue are locked and can be completed. At the same time, the exclusive lock also can support the bidirectional blocking feature well.

There are pros and cons in everything. The disadvantage is that because of an exclusive lock, you cannot perform two operations at the same time, which compromises performance. From the point of view of performance, Linkedblockingdeque is much lower than the Linkedblockingqueue, which is much lower than cocurrentlinkedqueue, which is more obvious in high concurrency.

Before analyzing enough queue implementations, the principle and implementation of Linkedblockingdeque is not worth mentioning, nothing more than an exclusive lock on a linked list of ordinary operations.

Interestingly, this class supports serialization, but node does not support serialization, so fist/last cannot serialize, so how do I complete the serialization/deserialization process?

Listing 1 serialization, deserialization of Linkedblockingdeque

private void WriteObject (Java.io.ObjectOutputStream s)
    throws java.io.IOException {
    Lock.lock ();
    try {
       //Write out capacity and any hidden stuff
        s.defaultwriteobject ();
       /Write out all elements in the proper order.
        for (node<e> p = first; P! = null; p = p.next)
             S.writeobject (P.item);
       /Use trailing null as Sentinel
         s.writeobject (NULL);
   } finally {
        lock.unlock ();
   }
}

private void ReadObject (Java.io.ObjectInputStream s)
Throws Java.io.IOException, ClassNotFoundException {
S.defaultreadobject ();
Count = 0;
first = null;
last = null;
Read in all elements and place in queue
for (;;) {
E item = (e) s.readobject ();
if (item = = NULL)
Break
Add (item);
}
}

Listing 1 describes the process of Linkedblockingdeque serialization/deserialization. The true element is written to the output stream when serialized, and finally a null is written. Read the list of all objects, if read to a null indicates the end. This is why a null is written when writing, because Count is not written to the stream, so the end is represented by NULL, which saves an integer space.

In layman's Java Concurrency (25): Concurrent container Part 10 bidirectional concurrent blocking queue blockingdeque[go]

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.