Self-implementing thread-safe queue based on Linkedblockingqueue source code

Source: Internet
Author: User

Linkedblockingqueue is a blocking, thread-safe, two-way queue implemented by a linked list, which, like Arrayblockingqueue, is the most common and most commonly used blocking queue. Now based on the Linkedblockingqueue source code self-realization of a one-way, simplified version of the Linkedblockingqueue.

 PackageCom.lzq.newInterview;Importjava.util.concurrent.locks.Condition;ImportJava.util.concurrent.locks.ReentrantLock; Public classMylinkedblockingqueue<e> {    PrivateNode<e> Head;//Team Head    PrivateNode<e> tail;//End of Team    Private Static Final intDefalt_size = 20; Private intSize//Maximum Queue Length    Private intCount//Queue Current Capacity    PrivateReentrantlock lock =NewReentrantlock (); PrivateCondition empty =lock.newcondition (); PrivateCondition full =lock.newcondition ();  PublicMylinkedblockingqueue () {head=NULL; Tail=NULL; Size=defalt_size; }     PublicMylinkedblockingqueue (intsize) {Head=NULL; Tail=NULL;  This. Size =size; }    //insert element at tail of queue     Public voidOffer (e e) {if(E = =NULL)            return; Try{lock.lock ();  while(Count = =size)            {full.await (); }            if(Head = =NULL&& tail = =NULL) {Node<E> node =NewNode<e> (E,NULL); Head=node; Tail=node; } Else{Node<E> node =NewNode<e> (E,NULL); Tail.next=node; Tail=node; } Count++; System.out.println ("Offer:count=" + This. Count);        Empty.signal (); } Catch(Exception exp) {exp.printstacktrace (); } finally{lock.unlock (); }    }     PublicE Poll () {Try{lock.lock ();  while(count = = 0) {empty.await (); } E OldValue=Head.value; Head=Head.next; if(Head = =NULL) Tail=NULL; Count--; System.out.println ("Poll:count=" + This. Count);            Full.signal (); returnOldValue; } Catch(Exception e) {e.printstacktrace (); } finally{lock.unlock (); }        return NULL; }    Static classNode<e>{Node<E>Next;        E value; Node (E Value, node<E>next) {             This. Value =value;  This. Next =Next; }    }     Public Static voidMain (string[] args) {Finalmylinkedblockingqueue<integer> queue =NewMylinkedblockingqueue<integer>();  for(inti = 0; I < 100; i++) {            Final intnum =i; Thread T=NewThread (NewRunnable () { Public voidrun () {queue.offer (num);            }            });        T.start (); }         for(inti = 0; I < 100; i++) {Thread T=NewThread (NewRunnable () { Public voidrun () {queue.poll ();            }            });        T.start (); }    }}

Partial Run Result:

Offer:count=1
offer:count=2
Offer:count=3
Offer:count=4
Offer:count=5
Offer:count=6
Offer:count=7
Offer:count=8
Offer:count=9
offer:count=10
offer:count=11
Offer:count=12
poll:count=11
poll:count=10
Poll:count=9
Poll:count=8
Poll:count=7
Poll:count=6
Poll:count=5
Poll:count=4
Poll:count=3
poll:count=2
Poll:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
offer:count=2
Offer:count=3
poll:count=2
Poll:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
offer:count=2
Poll:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
offer:count=2
Poll:count=1
Poll:count=0
Offer:count=1
offer:count=2
Poll:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
offer:count=2
Poll:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
Poll:count=0
Offer:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
offer:count=2
Poll:count=1
Poll:count=0

Self-implementing thread-safe queue based on Linkedblockingqueue source code

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.