Java concurrency, Blockingquene

Source: Internet
Author: User

Blockingqueue is also the main tool for controlling thread synchronization under Java.util.concurrent.

Blockingqueue has four specific implementation classes, depending on the needs, choose different implementation classes
1, Arrayblockingqueue: a bounded block queue supported by an array, specifying the size of the 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 variable blockingqueue, if its constructor with a specified size parameters, the resulting blockingqueue has a size limit, if not with the size of parameters, The size of the generated blockingqueue is determined by Integer.max_value. The objects it contains are sorted in FIFO (first in, first out) order.


3. Priorityblockingqueue: Similar to Linkedblockqueue, but the sort of objects it contains is not FIFO, but is determined by the natural sort order of the object or the comparator of the constructor.


4, Synchronousqueue: Special Blockingqueue, the operation of its must be put and take alternating completion.

Linkedblockingqueue can specify the capacity, or can not specify, if not specified, the default maximum is Integer.max_value, which is mainly used in the put and take method, the put method is blocked when the queue is full until a queue member is consumed, The Take method blocks when the queue is empty until a queue member is put in.

Sample code for producer consumers:

1  Packagecom.xt.thinks21_7;2 3 ImportJava.util.concurrent.BlockingQueue;4 ImportJava.util.concurrent.ExecutorService;5 Importjava.util.concurrent.Executors;6 ImportJava.util.concurrent.LinkedBlockingDeque;7 8 /**9 * Use Blockingquene to simulate producers and consumersTen  *  One  * @authorYmmmsick A  * -  */ -  Public classBlockingquenetest { the  -      Public Static voidMain (string[] args) { -Blockingqueue<string> Quene =NewLinkedblockingdeque<string> (2); -Executorservice es =Executors.newcachedthreadpool (); +          for(inti = 0; I < 10; i++) { -Es.execute (NewProduct (Quene, "thread->" +i)); +Es.execute (NewConsumer (Quene)); A         } at  - Es.shutdown (); -     } - } -  - classProductImplementsRunnable { in  -     PrivateBlockingqueue<string>Quene; to     PrivateString name; +  -      PublicProduct (blockingqueue<string>Quene, String name) { the          This. Quene =Quene; *          This. Name =name; $     }Panax Notoginseng  - @Override the      Public voidrun () { +         //TODO auto-generated Method Stub A         Try { the quene.put (name); +System.out.println ("Product:" +name); -}Catch(interruptedexception e) { $             //TODO auto-generated Catch block $ e.printstacktrace (); -         } -     } the  - }Wuyi  the classConsumerImplementsRunnable { -  Wu     PrivateBlockingqueue<string>Quene; -  About      PublicConsumer (blockingqueue<string>Quene) { $          This. Quene =Quene; -     } -  - @Override A      Public voidrun () { +         //TODO auto-generated Method Stub the         Try { -String T =Quene.take (); $System.out.println ("Consumer:" +t); the}Catch(interruptedexception e) { the             //TODO auto-generated Catch block the e.printstacktrace (); the         } -     } in  the}

Output Result:

Product:thread->0

Consumer:thread->0

Product:thread->1

Product:thread->2

Consumer:thread->1

Consumer:thread->2

Product:thread->3

Consumer:thread->3

Product:thread->4

Consumer:thread->4

Product:thread->5

Consumer:thread->5

Product:thread->6

Consumer:thread->6

Product:thread->7

Consumer:thread->7

Product:thread->8

Consumer:thread->8

Product:thread->9

Consumer:thread->9

Conclusion: Linkedblockingquene can only hold up to two objects in the queue during the same time period, the producer waits for blocking when the object is overflowing, and the consumer waits for blocking when the object is empty.

Java concurrency, Blockingquene

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.