Transferqueue and Synchronousqueue in Java 7

Source: Internet
Author: User

The JSR 166y specification was added to the JAVA7 to improve the collection class and the Concurrency class library. One of these is the addition of an interface TransferQueue and its implementation class LinkedTransferQueue .

Transferqueue BlockingQueue( BlockingQueue又 inherited Queue ) and expanded some new methods. Blockingqueue (and queue) is the interface added to Java 5, which refers to a queue where producers are blocked when the producer adds elements to the queue but the queue is full, and the consumer is blocked when the consumer removes the element from the queue but the queue is empty.

Transferqueue Further, the producer will block until the elements added to the queue are consumed by a single consumer (not just added to the queue). the newly added transfer method is used to implement this constraint. Blocking, as the name implies, occurs when an element is transfer from one thread to another, effectively implementing the transfer of elements between threads (in order to establish a happens-before relationship in the Java memory model).

Transferqueue also includes other methods: Two Trytransfer methods, one non-blocking, and the other with the timeout parameter setting time-out. There are also two helper methods Haswaitingconsumer () and Getwaitingconsumercount ().

When I first saw Transferqueue, I first thought of the existing implementation classes SynchronousQueue . Synchronousqueue has a queue length of 0, which I initially thought was of little use, but later I found it to be one of the most useful queue implementation classes in the entire Java Collection framework, especially for the use cases of passing elements between two threads.

Transferqueue is much more useful than synchronousqueue, because you can decide whether to use the Blockingqueue method (the translator notes: For example, the Put method) or to ensure that one pass is complete (the translator notes: the Transfer method). In the case of an existing element in the queue, calling the transfer method ensures that all elements before the passed element in the queue can be processed. Doug Lea says from a functional point of view, LinkedTransferQueue is actually a superset of Concurrentlinkedqueue, Synchronousqueue (fair mode) and Linkedblockingqueue. And LinkedTransferQueue is better, because it not only synthesizes the functions of these classes, but also provides a more efficient implementation.

Joe Bowbeer offers a paper by William Scherer, Doug Lea, and Michael Scott, which shows the linkedtransferqueue algorithm in this paper, and the performance test results show that it is better than Java 5 of those classes (translator Note: Concurrentlinkedqueue, Synchronousqueue, and Linkedblockingqueue). The performance of LinkedTransferQueue is 3 times times of synchronousqueue (non-fair mode) and 14 times times (fair mode) respectively. Because classes like Threadpoolexecutor use Synchronousqueue when the task is passed, Therefore, the use of LinkedTransferQueue to replace the Synchronousqueue will also make threadpoolexecutor get corresponding performance improvement. Given the importance of executor in concurrent programming, you will understand the importance of adding this implementation class.

the Synchronousqueue in Java 5 uses two queues (one for the waiting producer, another for the waiting consumer), and a lock to protect two queues . LinkedTransferQueue uses CAS operations (translator Note: reference wiki) to implement a non-blocking approach, which is the key to avoiding serialization processing tasks. This paper also lists a lot of details and data that, if you are interested, are well worth reading.

Synchronousqueue

Synchronousqueue is a blocking queue in which each put must wait for a take and vice versa . the synchronization queue does not have any internal capacity, and even one queue has no capacity.
Peek cannot be performed on the synchronization queue because the element exists only when attempting to get the element;
You cannot add elements (using any method) unless another thread attempts to remove an element.
You cannot iterate the queue because there are no elements in it that can be used for iterations. The header of the queue is the first queued thread element that is attempted to be added to the queue, and if there are no queued threads, the element is not added and the header is null.
For other Collection methods (for example, contains), Synchronousqueue as an empty collection. This queue does not allow null elements.
Synchronization queues are similar to the rendezvous channels used in CSP and Ada.
It is ideal for transitive designs in which objects running in a thread will have some information,
An event or task is passed to an object that is running in another thread, and it must be synchronized with the object.
For the producer and consumer threads that are waiting, this class supports an optional fair ordering policy. This sort is not guaranteed by default.
However, queues constructed with fair set to true ensure that threads are accessed in a FIFO order. Fairness usually lowers throughput, but it can reduce variability and avoid service loss.

constructor function

Note 1: It is a blocking queue in which each put must wait for a take, and vice versa.
the synchronization queue does not have any internal capacity, and even one queue has no capacity.
NOTE 2: It is thread-safe and is blocked.
NOTE 3: null elements are not allowed.
Note 4: The Fair sort policy refers to the call between put threads, or take between threads.
a fair ranking strategy can be used to examine the fairness strategy in Arrayblockingqueue.
Note the following methods of 5:synchronousqueue are interesting:
* Iterator () always returns empty because there is nothing inside.
* PEEK () always returns NULL.
* PUT () put an element into the queue and wait until another thread comes in and takes the element away.
* Offer () returns immediately after placing an element in the queue, and if it happens that the element was taken away by another thread, the Offer method returns true to be successful;
* Offer (Timeunit.seconds) put an element in the queue but wait for the specified time to return, just as the logical and offer () method is returned.
* Take () Remove and remove the element from the queue (thought to be in the queue ...). ), he will wait until he can get nothing.
* Poll () Remove and remove the element in the queue (thought to be in the queue ...). ), the method will fetch only when it happens that another thread is in the queue to offer data or put data. Otherwise, NULL is returned immediately.
* Poll (Timeunit.seconds) waits for the specified time and then takes out and removes the element in the queue, in fact, waiting for other thread to go in and out.
* IsEmpty () is always true.
* Remainingcapacity () is always 0.
* Remove () and RemoveAll () are always false.

Transferred from: http://blog.csdn.net/hudashi/article/details/7076814

http://ifeve.com/java-transfer-queue/

Http://www.cnblogs.com/wangzhongqiu/p/6441703.html

Transferqueue and Synchronousqueue in Java 7

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.