BlockingQueue using Java to achieve producer - consumer

BlockingQueue java.util.concurrent is mainly used in the thread synchronization tool of control.

BlockingQueue four specific category, according to different needs, to select a different implementation class
1, ArrayBlockingQueue: backed by an array of a bounded blocking queue BlockingQueue predetermined size, which must take a constructor parameter to indicate the size int. objects it contains are FIFO (first in first out) sort order.


2, LinkedBlockingQueue:. BlockingQueue varying sizes, if its constructor parameter with a predetermined size, the resulting BlockingQueue limited in size, if the size of the parameters with the generated BlockingQueue determined by the size of which contains Integer.MAX_VALUE objects are FIFO (first in first out) sort order.


3, PriorityBlockingQueue: similar LinkedBlockQueue, but not the sort object contained FIFO, but the order of the sort order of the objects Comparator natural or determined based constructor.


4, SynchronousQueue: special BlockingQueue, its operation must take place alternately and completed.

 

LinkedBlockingQueue can be specified capacity, can not specified, not specified, the default maximum is Integer.MAX_VALUE, mainly used put and take methods, methods put in the queue is full time members will block until the queue is consumed, take the method in the queue air time will block until the queue is put to members.

 

Producers and consumers of sample code:

Producer:

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, 生产线程:"
+ Thread.currentThread().getName();
System.out.println("I have made a product:"
+ Thread.currentThread().getName());
queue.put(temp);//如果队列是满的话,会阻塞当前线程
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
 

 consumer:

java.util.concurrent.BlockingQueue Import;

public class Consumer the implements the Runnable {
BlockingQueue <String> Queue;

public Consumer (BlockingQueue <String> Queue) {
this.queue = Queue;
}

@Override
public void RUN () {
the try {
String TEMP = queue.take (); // if the queue is empty, the current thread will block
System.out.println (TEMP);
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
}
 test classes:

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>();
//不设置的话,LinkedBlockingQueue默认大小为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();
}
}
}
 Print result:

The I have have Made A Product: producerl
the I have have Made A Product: Producer2
A Product, production thread: producerl
A Product, production thread: Producer2
the I have have Made A Product: Producer3
A Product, production thread: Producer3
the I have have Made A Product: Producer5
the I A Product Made have have: Producer4
A Product, production thread: Producer5
A Product, production thread: Producer4
 

Since the size of the queue is defined became 2, so that a maximum of only two products which are added to the queue, and the ordering consumer products is to take and is LinkedBlockingQueue ArrayBlockingQueue access elements are produced in accordance with the order of FIFO in the order, the reason of.
---------------------
Disclaimer: This article is CSDN blogger "micky2046 'original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/micky2046/article/details/84476180

Guess you like

Origin www.cnblogs.com/renjiaqi/p/11324119.html
Recommended