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:
Producers:
Java code
- import java.util.concurrent.BlockingQueue;
- Public class Producer implements Runnable {
- Blockingqueue<string> queue;
- Public Producer (blockingqueue<string> queue) {
- this. Queue = queue;
- }
- @Override
- Public void Run () {
- Try {
- String temp = "A product, production line:"
- + Thread.CurrentThread (). GetName ();
- System.out.println ("I have made a product:"
- + Thread.CurrentThread (). GetName ());
- Queue.put (temp); //If the queue is full, it will block the current thread
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- }
- }
Import Java.util.concurrent.blockingqueue;public class Producer implements Runnable {blockingqueue<string> Queue;public Producer (blockingqueue<string> queue) {this.queue = queue;} @Overridepublic void Run () {try {String temp = "A product, Production line:" + thread.currentthread (). GetName (); System.out.println ("I have made a product:" + thread.currentthread (). GetName ()); Queue.put (temp);//If the queue is full, it blocks the current thread} catch (Interruptedexception e) {e.printstacktrace ();}}}
Consumers:
Java code
- import java.util.concurrent.BlockingQueue;
- Public class Consumer implements runnable{
- Blockingqueue<string> queue;
- Public Consumer (blockingqueue<string> queue) {
- this. Queue = queue;
- }
- @Override
- Public void Run () {
- Try {
- String temp = Queue.take (); //If the queue is empty, the current thread is blocked
- SYSTEM.OUT.PRINTLN (temp);
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- }
- }
Import Java.util.concurrent.blockingqueue;public class Consumer implements Runnable{blockingqueue<string> Queue;public Consumer (blockingqueue<string> queue) {this.queue = queue;} @Overridepublic void Run () {try {String temp = Queue.take ();//If the queue is empty, it blocks the current thread System.out.println (temp);} catch ( Interruptedexception e) {e.printstacktrace ();}}}
Test class:
Java code
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.LinkedBlockingQueue;
- Public class TEST3 {
- Public Static void Main (string[] args) {
- blockingqueue<string> queue = new linkedblockingqueue<string> (2);
- //blockingqueue<string> queue = new linkedblockingqueue<string> ();
- //Not set, linkedblockingqueue default size is Integer.max_value
- //blockingqueue<string> queue = new arrayblockingqueue<string> (2);
- Consumer Consumer = new Consumer (queue);
- Producer Producer = new Producer (queue);
- for (int i = 0; i < 5; i++) {
- New Thread (producer, "producer" + (i + 1)). Start ();
- New Thread (Consumer, "consumer" + (i + 1)). Start ();
- }
- }
- }
Import Java.util.concurrent.arrayblockingqueue;import Java.util.concurrent.blockingqueue;import Java.util.concurrent.linkedblockingqueue;public class Test3 {public static void main (string[] args) {blockingqueue< string> queue = new linkedblockingqueue<string> (2);//blockingqueue<string> queue = new Linkedblockingqueue<string> ();//If not set, linkedblockingqueue default size is integer.max_value//blockingqueue<string > queue = new arrayblockingqueue<string> (2); Consumer Consumer = new Consumer (queue); Producer Producer = new Producer (queue), for (int i = 0; i < 5; i++) {New Thread (Producer, "Producer" + (i + 1)). Start () ; New Thread (Consumer, "consumer" + (i + 1)). Start ();}}
Printing results:
Text Code
- i have made a product:producer1
- i have made a& nbsp;product:producer2
- a product, production line:producer1
- A product, production line:producer2
- i have made a product:producer3
- a product, production line:producer3
- i have made a product:producer5
- i have made a product:producer4
- a product, production line:producer5
- a product, production line:producer4
I have made a product:producer1i has made a PRODUCT:PRODUCER2A product, production line: Producer1a product, production line: Producer2i has mad e a PRODUCT:PRODUCER3A product, production line: Producer3i has made a product:producer5i has made a PRODUCT:PRODUCER4A product, production line Process: producer5a product, production line: Producer4
Since the size of the queue is limited to 2, up to two products are added to the queue, and the order in which consumers take the product is in order of production, The reason is that both linkedblockingqueue and arrayblockingqueue are accessing elements in FIFO order.
Using Java's blockingqueue to implement producer-consumer