Linkedblockingqueue is a blocking queue consisting of a list of links, first to see demo
public class Linkedblockingqueuedemo {public static void main (string[] args) {Executorservice es = executors.newcachedthr Eadpool (); blockingqueue<bread> queue = new linkedblockingqueue<bread> (int i = 0; i < 2; i++) {Es.execute (new B Aker (queue));} for (int i = 0; i < i++) {Es.execute (new Breadconsumer (queue));} Es.shutdown ();}} Class Baker implements runnable{private static int no = 0;private int id = ++no;private int count = 0;private Blockingqueu E<bread> queue;public Baker (blockingqueue<bread> queue) {this.queue = queue;} @Overridepublic void Run () {for (int i = 0; i < i++) {System.out.println ("Baker" + ID + "Preparing for the first" + ++count + "bread"); Bread Bread = new Bread ();//Full queue case, block try {queue.put (Bread); System.out.println ("Baker" + ID + "Do the first" + Count + "bread is bread" + bread.getid ());} catch (Interruptedexception e) {}}}}class Breadconsumer implements runnable{private static int no = 0;private int id = ++n o;private int count = 0;private blockingqueue<bread> queue;pUblic Breadconsumer (blockingqueue<bread> queue) {this.queue = queue;} @Overridepublic void Run () {for (int i = 0; i < 2; i++) {System.out.println ("customer" + ID + "ready to buy" + ++count + "bread"); Bread Bread = null;//Empty queue case, block try {Bread = Queue.take ();} catch (Interruptedexception e) {}<span style= "white-space:pr E "></span>system.out.println (" customer "+ ID +" purchased "+count+" bread is bread "+ bread.getid ());}} Class Bread {private static int count = 0;private int id = ++count;public int getId () {return id;}}
By default, its capacity is Integer.max_value
Public Linkedblockingqueue () {This (integer.max_value); }
You can also specify a capacity to avoid over-expansion of the list
public linkedblockingqueue (int capacity) { if (capacity <= 0) throw new IllegalArgumentException (); This.capacity = capacity; last = head = new node<e> (null); }
In the Linkedblockingqueue's list, there are two pointers, pointing to the queue header and the tail of the queue, respectively. Unlike Arrayblockingqueue, in Linkedblockingqueue, there are two locks, each locking the queue head and the tail of the queue. It's good to be able to queue up and out of the team at the same time, higher performance than arrayblockingqueue
/** Chain head */ private transient node<e> head; /** chain Footer */ private transient node<e> last; /** out of the team's lock * /private final Reentrantlock Takelock = new Reentrantlock (); /** Wait queue for waiting takes * /private final Condition Notempty = Takelock.newcondition (); /** Queue Lock * /private final Reentrantlock Putlock = new Reentrantlock (); /** Wait queue for waiting puts * /private final Condition Notfull = Putlock.newcondition ();
Let's look at the put blocking method, and if the queue is full, it will block until the queue is dissatisfied
public void put (e e) throws interruptedexception { if (E = = null) throw new NullPointerException (); int c =-1; Final Reentrantlock putlock = This.putlock; Final Atomicinteger count = This.count; Putlock.lockinterruptibly (); try { while (count.get () = = capacity) { notfull.await (); } Enqueue (e); c = count.getandincrement (); if (c + 1 < capacity) notfull.signal (); } finally { putlock.unlock (); } if (c = = 0) signalnotempty (); }
When inserting data into a queue, the Putlock lock is first held, if the number of current queue elements is equal to the capacity, blocking, calling Notfull.await. Otherwise, queue up. If the number of elements is smaller than the queue capacity, other blocked insert threads are awakened. The last sentence, do not understand, the element is 0, why do you want to perform the wake empty condition? Ask for advice
In the queue, insert the element, insert the end of the team
private void Enqueue (E x) { //Assert Putlock.isheldbycurrentthread (); last = Last.next = new node<e> (x); }
To see the team operation take, but also the blocking method
Public E take () throws Interruptedexception { e x; int c =-1; Final Atomicinteger count = This.count; Final Reentrantlock takelock = This.takelock; Takelock.lockinterruptibly (); try { while (count.get () = = 0) { notempty.await (); } x = Dequeue (); c = count.getanddecrement (); if (C > 1) notempty.signal (); } finally { takelock.unlock (); } if (c = = capacity) signalnotfull (); return x; }
The team gets the Takelock, similarly, if the number of current queue elements is 0, block, call notempty.await. Otherwise, out of the team. If the number of elements is greater than 0 after the queue, the other blocked outbound threads are awakened. Get out of the team and get out of the squad.
Private E dequeue () { //Assert Takelock.isheldbycurrentthread (); Node<e> h = head; node<e> first = H.next; H.next = h; Help GC head = first; E x = First.item; First.item = null; return x; }
Java Concurrency Learning Note (eight)-linkedblockingqueue