This example is just a few small practices that bloggers learn to block queues and post, not real application scenarios!
The producer consumer scenario is the most common scenario in our application, where we can implement producer and consumer scenarios through reentrantlock condition and thread wait,notify and communication to achieve multi-producer and multi-consumer patterns, The latter can only achieve one producer, one consumer model.
Today we use the blocking queue to implement the producer and consumer patterns (which also use the thread pool).
I've seen my friends on the Block queue blog, I know, the blocking queue is actually implemented by Reentrantlock!
The scene is not described, for a simple multi-producer and multi-consumer!
On the code:
Process line:
public class ProductThread extends Thread { private int taskNum; private ArrayBlockingQueue queue; public ProductThread(int taskNum,ArrayBlockingQueue queue) { this.taskNum = taskNum; this.queue = queue; } public void run() { try { //模拟生产 Thread.currentThread().sleep(5000); System.out.println("开始生产"); queue.add(taskNum); } catch (InterruptedException e) { e.printStackTrace(); } }}
Consumer Threads:
public class ConsumerThread extends Thread { private ArrayBlockingQueue queue; public ConsumerThread(ArrayBlockingQueue queue) { this.queue = queue; } public void run() { System.out.println("准备消费"); int taskNum; try { taskNum = (int) queue.take(); System.out.println("消费了"+taskNum); } catch (InterruptedException e) { e.printStackTrace(); } }}
Main thread (Master method):
public class Productandconsumer {public static void main (string[] args) {arrayblockingqueue< integer> queue = new arrayblockingqueue<integer> (20); Thread pool created for multi-producer and multi-consumer threadpoolexecutor Productpool = new Threadpoolexecutor (10,20,60,timeunit.mill Iseconds,new Arrayblockingqueue (5), New Threadpoolexecutor.callerrunspolicy ()); Threadpoolexecutor Consumerpool = new Threadpoolexecutor (10,20,60,timeunit.milliseconds,new ArrayBlockingQ Ueue (5), New Threadpoolexecutor.callerrunspolicy ()); System.out.println ("Start"); for (int i = 0;i<100;i++) {productpool.execute (new Productthread (I,queue)); Consumerpool.execute (new Consumerthread (queue)); } productpool.shutdown (); Consumerpool.shutdown (); }}
The above code, I opened a thread pool for producers and consumers, because in the actual application may appear the producer and the consumer is not equal, so we should according to the actual situation to set the thread pool parameters to adapt to different scenarios!
In the creation of the thread pool to add a saturated process, we wrote the last section of the thread pool saturation processing when it is mentioned that, if no parameters, will default to throw exception, I select the saturation strategy is to the saturation of the task to the calling thread (i.e. the main thread) processing, this should be based on the actual situation to be determined, We can also implement the Rejectedexecutionhandler interface to define this saturation anomaly by ourselves!
The above is only the blogger to learn their own examples of writing, may be considered in some cases is not thoughtful, please advise!
2018.4.18 21:31
Simple implementation of producer and consumer scenarios with blocking queues and thread pools