Java. util. Concurrent. Define blockingqueue

Source: Internet
Author: User
I. Introduction

Linkedblockingqueue is one of blockingqueue's implementations using the link list. It uses two different locks for the first and last (fetch and add operations), which improves the throughput compared with arrayblockingqueue. It is also a blocking container and is suitable for implementing the "Consumer producer" mode.

II. Specific implementation

The underlying definition of linkedblockingqueue is as follows:

Public class implements blockingqueue <E> extends implements actqueue <E> implements blockingqueue <E>, Java. io. serializable {static class node <e >{/ ** the item, volatile to ensure barrier separating write and read */volatile e item; node <E> next; node (e x) {item = x ;}/// supports atomic operation private final atomicinteger COUNT = new atomicinteger (0); // The head and end of the chain table private transient node <E> head; private transient node <E> last; // Private Final reentrantlock takelock = new reentrantlock (); private final condition notempty = takelock. newcondition (); private final reentrantlock putlock = new reentrantlock (); private final condition notfull = putlock. newcondition ();...}

Add the linkedblockingqueue:

 

Public class implements blockingqueue <E> extends implements actqueue <E> implements blockingqueue <E>, Java. io. serializable {private void insert (e x) {last = last. next = new node <E> (x);}/*** when the signal method is called, the current thread must own the condition-related lock! * Signal a waiting Take. called only from put/offer (which do not * Otherwise ordinarily lock takelock .) */private void signalnotempty () {final reentrantlock takelock = This. takelock; takelock. lock (); try {notempty. signal ();} finally {takelock. unlock () ;}} public void put (e o) throws interruptedexception {If (O = NULL) throw new nullpointerexception (); int c =-1; final reentrantlock putlock = This. putlock; Final atomicinteger COUNT = This. count; // use putlock. lockinterruptibly (); try {// when the capacity is full, wait for the notfull condition while (count. get () = capacity) notfull. await ();} catch (interruptedexception IE) {notfull. signal (); // propagate to a non-interrupted thread throw ie;} insert (o); // retrieve the current value and Add 1 c = count to the original data. getandincrement (); // The capacity is not enough. Activate the put thread if (C + 1 <capacity) notfull on notfull again. signal ();} finally {putlock. unlock () ;}// putlock must be released first and then signal on notempty; otherwise, the deadlock if (C = 0) signalnotempty ();}...}

Fetch blockingqueue:

Public class implements blockingqueue <E> extends implements actqueue <E> implements blockingqueue <E>, Java. io. serializable {private e extract () {node <E> first = head. next; head = first; e x = first. item; first. item = NULL; return X;} private void signalnotfull () {final reentrantlock putlock = This. putlock; putlock. lock (); try {notfull. signal ();} finally {putlock. unlock () ;}} public e take () throws interruptedexception {e x; int c =-1; Final atomicinteger COUNT = This. count; Final reentrantlock takelock = This. takelock; // use takelock. lockinterruptibly (); try {// if the capacity is empty, wait for notempty while (count. get () = 0) notempty. await ();} catch (interruptedexception IE) {notempty. signal (); // propagate to a non-interrupted thread throw ie;} X = extract (); C = count. getanddecrement (); // activate notempty if (C> 1) notempty again. signal ();} finally {takelock. unlock () ;}// if the capacity is full before the take operation, activate notfull if (C = capacity) signalnotfull (); Return x ;}...}

 

Concurrentincluqueue is a non-lock queue implementation. It adopts a non-lock algorithm (as described in the API), which greatly improves the throughput compared with the conventional synchronized queue, at the same time, it is different from blockingqueue and does not only provide blocking operations. It mainly aims to improve the container throughput by adopting the lock-free algorithm so that the read/write operations do not need to lock the container.

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.