Linkedblockingqueue:
Public class extends Implements Blockingqueue<e>, Serializable
This queue sorts elements by FIFO (first-in, in-out). The head of the queue is the longest element in the queue. The tail of the queue is the element with the shortest time in the queue. The new element is inserted at the end of the queue, and the queue retrieval operation obtains the element at the head of the queue.
The throughput of a linked queue is typically higher than an array-based queue, but in most concurrent applications, its predictable performance is low. The optional capacity range constructs method parameters as a way to prevent the queue from over-scaling. If no capacity is specified, it is equal to Integer.max_value. The link node is created dynamically each time it is inserted, unless the node is inserted to make the queue out of capacity.
Benefits of the blocking queue: multi-threaded operations in a common queue do not require additional synchronization, the other is that the queue automatically balances the load, that is, the side (production and consumption on both sides) processing faster will be blocked, thereby reducing the processing speed gap on both sides.
Demo:
ImportJava.util.concurrent.BlockingQueue;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.LinkedBlockingQueue; Public classMain { Public Static voidMain (string[] args) {//build a basket of applesBasket Basket =NewBasket (); Executorservice Service=Executors.newcachedthreadpool (); Producer Producer=NewProducer ("producer 001", basket); Producer Producer2=NewProducer ("producer 002", basket); Consumer Consumer=NewConsumer ("Consumer 001", basket); Service.submit (producer); Service.submit (PRODUCER2); Service.submit (consumer); //after the program runs 5s, all tasks stop Try{Thread.Sleep (1000 * 5); } Catch(interruptedexception e) {e.printstacktrace (); } service.shutdownnow (); }}/*** * Definition of apple basket **/classBasket {//Basket, capable of accommodating up to 3 applesBlockingqueue<string> Basket =NewLinkedblockingqueue<string> (3); intI=0; //produce apples, put in baskets Public voidProduce ()throwsinterruptedexception {//Put the method into an apple, if the basket full, until basket have a positioni++; Basket.put ("An apple" +i); } //consume apples and take them out of the basket PublicString consume ()throwsinterruptedexception {//Take method Take out an apple, if basket is empty, wait until basket has an apple (gets and removes the head of this queue) returnBasket.take (); }}//define Apple producersclassProducerImplementsRunnable {PrivateString instance; PrivateBasket Basket; PublicProducer (String instance, basket basket) { This. Instance =instance; This. Basket =Basket; } Public voidrun () {Try { while(true) { //Production of ApplesSystem.out.println ("producer ready to produce apples:" +instance); Basket.produce (); System.out.println ("! Producer production Apple finished:" +instance); //Sleep 300msThread.Sleep (300); } } Catch(Interruptedexception ex) {System.out.println ("Producer interrupted"); } }}//define Apple ConsumerclassConsumerImplementsRunnable {PrivateString instance; PrivateBasket Basket; PublicConsumer (String instance, basket basket) { This. Instance =instance; This. Basket =Basket; } Public voidrun () {Try { while(true) { //Consumer AppleSYSTEM.OUT.PRINTLN ("Consumer ready to consume Apple:" +instance); System.out.println (Basket.consume ()); System.out.println ("Consumer consumption Apple finished:" +instance); //Sleep 1000msThread.Sleep (1000); } } Catch(Interruptedexception ex) {System.out.println ("Consumer interrupted"); } }}
producers prepare to produce Apple: 001 Consumers prepare to consume Apple: 001 producers prepare to produce Apple: Producer 002!producer Production Apple finished: Producer 002!producer produces Apple finished: Producer 001An Apple1!Consumer consumption Apple finished: Consumer 001 producers ready to produce Apple: Producer 001 producers ready to produce Apple: Producer 002!producer Production Apple finished: Producer 001!producer Production Apple finished: Producer 002 Producers ready to produce Apple: Producer 001 producers ready to produce Apple: Producer 002 Consumer ready to consume Apple: consumer 001An apple2!producer Production Apple finished: Producer 001!Consumer consumption Apple finished: Consumer 001 producers ready to produce Apple: 001 Consumers prepare to consume Apple: consumer 001An apple3!Consumer consumption Apple finished: Consumer 001!producer Production Apple finished: Producer 002 Producers ready to produce Apple: Producer 002 Consumer ready to consume Apple: consumer 001An apple4!Consumer consumption Apple finished: Consumer 001!producer Production Apple finished: Producer 001 producers ready to produce Apple: Producer 001 Consumer ready to consume Apple: consumer 001An apple5!Consumer consumption Apple finished: Consumer 001!producer Production Apple finished: Producer 002 Producers ready to produce Apple: Producer 002Producer interruptedproducer Interruptedconsumer interrupted
View Code
Http://www.cnblogs.com/linjiqin/archive/2013/05/30/3108188.html
http://blog.csdn.net/ac903919/article/details/6967728
Blocking queue Linkedblockingqueue and concurrent queue Concurrentlinkedqueue