blocking queues in Java

Source: Internet
Author: User

The JDK7 provides 7 blocking queues, as follows:

Arrayblockingqueue: a bounded block queue consisting of an array structure.

Linkedblockingqueue: A bounded blocking queue consisting of a list structure.

Priorityblockingqueue: A unbounded blocking queue that supports priority ordering.

Delayqueue: An unbounded blocking queue implemented with a priority queue.

Synchronousqueue: A blocking queue that does not store elements.

LinkedTransferQueue: An unbounded blocking queue consisting of a linked list structure.

Linkedblockingdeque: A two-way blocking queue consisting of a linked list structure.

Several queues are described below:

1.ArrayBlockingQueue

  Arrayblockingqueue is a bounded blocking queue of array structures that sorts elements according to the FIFO principle.

By default, thread-fair access queues are not guaranteed, so-called fair access queues refer to blocked threads that can access the queue in the order of blocking, that is, blocking the line enters upgradeable access queue first. Unfairness is the right to wait for a thread that is not fair, and when the queue is available, the blocked thread can compete for the queue's eligibility, and it is possible for the queue to be blocked at the end before the queue is accessed. In order to ensure fairness, the throughput is usually reduced. The following code can create a fair blocking queue:

New Arrayblockingqueue (+,true);

The fairness of the visitor is achieved through a reentrant lock, with the following constructor:

 public  arrayblockingqueue (int  Capacity, boolean   fair) { if  (capacity <= 0 throw         new   IllegalArgumentException ();  this . Items = new   object[capacity];        Lock  = new   Reentrantlock (fair);        Notempty  = lock.newcondition ();    Notfull  = lock.newcondition (); }

Arrayblockingqueue uses an object array to hold the data, an int type of count representing the number of elements added to the current queue, two condition objects bounded by the guaranteed dependency, and a look at the put () method, the code is as follows:

 Public voidPut (e e)throwsinterruptedexception {checknotnull (e); FinalReentrantlock lock = This. Lock;        Lock.lockinterruptibly (); Try {            //determine if the queue is full             while(Count = =items.length)//if it's full, waitnotfull.await (); //InsertInsert (e); } finally{lock.unlock (); }}Private voidInsert (E x) {Items[putindex]=x; Putindex= Inc (PUTINDEX);//plus 1++count; Notempty.signal ();}

Arrayblockingqueue when a put operation is performed, first acquires the lock, then determines whether the insert queue is full, waits if the queue is full, otherwise smoothly inserts, and executes a notempty.signal () wake-up with the possibility that the queue is empty to execute take () operation on the waiting thread, Take () method code is as follows:

 PublicE Take ()throwsinterruptedexception {FinalReentrantlock lock = This. Lock;    Lock.lockinterruptibly (); Try {        //if the queue is empty, wait for the element to be enqueued         while(count = = 0) notempty.await (); returnextract (); } finally{lock.unlock (); }}PrivateE Extract () {Finalobject[] Items = This. Items; E x= This.<e>cast (Items[takeindex]); Items[takeindex]=NULL; Takeindex=Inc (TAKEINDEX); --count;    Notfull.signal (); returnx;}

The take operation also determines whether the queue is empty, waits for null, does not empty, and returns Items[takeindex], and executes notfull.signal () wakes the thread that may be waiting for the put operation.

2.LinkedBlockingQueue

  

blocking queues in Java

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.