ImportJava.util.concurrent.ArrayBlockingQueue;ImportJava.util.concurrent.BlockingQueue;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;/**This example introduces a special queue: Blockingqueue, if Blockqueue is empty, the operation from the Blockingqueue will be blocked into the waiting state until the blockingqueue into something will be awakened. Similarly, If the blockingqueue is full, any attempt to store something in it will be blocked into the waiting state until there is space in the blockingqueue to be woken up to continue operation. This example again implements the 11.4 thread----The basket program described in condition, but the maximum number of apples in this basket is not 1, which can be arbitrarily specified. When the basket is full, the producer enters the waiting state, and when the basket is empty, the consumer waits. *//** Key technical points for using Blockingqueue are as follows: The common method of 1.BlockingQueue definition is as follows: 1) Add (anobject): Add AnObject to Blockingqueue, that is, if Blockingqueu E can accommodate, or return true, otherwise recruitment exception 2) offer (AnObject): Indicates that if possible, add AnObject to Blockingqueue, that is, if Blockingqueue can accommodate, returns True, Otherwise, false is returned. 3) put (anobject): Add AnObject to Blockingqueue, if Blockqueue has no space, then the thread calling this method is blocked until the blockingqueue has space to continue. 4) Poll (time): Take the Blockingqueue in the first row of the object, if not immediately remove, you can wait for the times specified by the parameters, not to return NULL 5) Take (): Taking the Blockingqueue in the first line of the object, If Blockingqueue is empty, block into wait until blocking has new object to join 2.BlockingQueue has four specific implementation classes, depending on the requirements, choose different implementation Class 1) Arrayblockingqueue: Specified size Blockingqueue, its constructor must take an int parameter to indicate its size. The objects it contains are sorted in FIFO (first in, first out) order. 2) Linkedblockingqueue: The size of the blockingqueue, if its constructor with a specified size parameter, the resulting blockingqueue has a size limit, if not with the size parameter, The size of the generated blockingqueue is determined by Integer.max_value. It contains objects that are sorted in FIFO (first in, first out) Order of 3) Priorityblockingqueue: Similar to Linkedblockqueue, But the sort of objects it contains is not FIFO, but is based on the natural sort order of the object or the order of the comparator decision of the constructor function. 4) Synchronousqueue: Special Blockingqueue, the operation of which must be put and take the alternate completion. 3.LinkedBlockingQueueCompared with arrayblockingqueue, the data structure behind them is different, resulting in linkedblockingqueue data throughput greater than arrayblockingqueue. However, when the number of threads is large, its performance is less predictable than arrayblockingqueue.*/ Public classBlockingqueuetest {/**Define baskets for apples*/ Public Static classbasket{//Basket, capable of accommodating up to 3 applesBlockingqueue<string> Basket =NewArrayblockingqueue<string> (3); //produce apples, put in baskets Public voidProduce ()throwsinterruptedexception{//Put the method into an apple, if the basket full, until basket have a positionBasket.put ("An apple"); } //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 returnBasket.take (); } } //test Method Public Static voidTestbasket () {FinalBasket Basket =NewBasket ();//build a basket of apples//define Apple producers classProducerImplementsrunnable{ Public voidrun () {Try{ while(true){ //Production of ApplesSystem.out.println ("producer ready to produce apples:" +System.currenttimemillis ()); Basket.produce (); System.out.println ("Producer production Apple finished:" +System.currenttimemillis ()); //Sleep 300msThread.Sleep (300); } }Catch(Interruptedexception ex) {}}} //define Apple Consumer classConsumerImplementsrunnable{ Public voidrun () {Try{ while(true){ //Consumer AppleSYSTEM.OUT.PRINTLN ("Consumer ready to consume Apple:" +System.currenttimemillis ()); Basket.consume (); System.out.println ("Consumer consumer Apple finished:" +System.currenttimemillis ()); //Sleep 1000msThread.Sleep (1000); } }Catch(Interruptedexception ex) {}}} Executorservice Service=Executors.newcachedthreadpool (); Producer Producer=NewProducer (); Consumer Consumer=NewConsumer (); Service.submit (producer); Service.submit (consumer); //after the program runs 5s, all tasks stop Try{Thread.Sleep (5000); }Catch(Interruptedexception ex) {} service.shutdownnow (); } Public Static voidMain (string[] args) {blockingqueuetest.testbasket (); }}
Original: http://blog.csdn.net/azhao_dn/article/details/8191820
Java Blocking queue