What is a blocking queue
A blocking queue (BlockingQueue) is a queue that supports two additional operations. These two additional operations support blocking insertion and removal methods.
- 1. Support blocking insertion method: It means that when the queue is full, the queue will block the thread that inserts the element until the queue is unsatisfied.
- 2. Support blocking removal method: It means that when the queue is empty, the thread that obtains the element will wait for the queue to become non-empty.
Blocking queues are often used in scenarios for producers and consumers. Producers are threads that add elements to queues, and consumers are threads that take elements from queues. A blocking queue is a container used by producers to store elements and consumers to obtain elements.
Java provides several implementation classes for BlockingQueue, such as: ArrayBlockingQueue, LinkedBlockingQueue, PriorityBlockingQueue, SynchronousQueue, etc.
When dealing with producer/consumer issues, we will use ArrayBlockingQueue to implement it. Here are the important methods we need to know:
- put(E e):This method is used to insert elements into the queue. If the queue is full, you need to wait for the available room.
- E take():This method is used to obtain or remove elements from the queue header. If the queue is empty, you need to wait for available elements.
Use BlockingQueue to solve producer/consumer Example
Mantou Class
Normal Java objects generated by Producer and added to the queue.
/** * Steamed buns produced by Producer * @author itmyhome * */ public class Mantou { private String mantou; public Mantou(String mantou) { = mantou; } public String getMantou() { return mantou; } public void setMantou(String mantou) { = mantou; } }
Producer Producer
The Producer class will generate messages and put them in the queue.
import ; public class Producer implements Runnable { BlockingQueue<Mantou> queue; public Producer(BlockingQueue<Mantou> queue) { = queue; } @Override public void run() { // Produce steamed buns for (int i = 0; i < 100; i++) { Mantou mt = new Mantou("" + i); try { (100); (mt); ("Produce steamed buns: " + ()); } catch (InterruptedException e) { (); } } // Add an exit message Mantou msg = new Mantou("exit"); try { (msg); } catch (InterruptedException e) { (); } } }
Consumer Consumer
The Consumer class will get messages from the queue for processing. If the exit message is obtained, it ends.
import ; public class Consumer implements Runnable { BlockingQueue<Mantou> queue; public Consumer(BlockingQueue<Mantou> queue) { = queue; } @Override public void run() { try { Mantou mantou; // Get and process the message until the "exit" message is received while (!(mantou = ()).getMantou().equals("exit")) { (100); ("Consuming steamed buns: " + ()); } } catch (InterruptedException e) { (); } } }
ProducerConsumerService
The producer/consumer service class will generate a fixed-size BlockingQueue, and the producer and consumer share the BlockingQueue at the same time. The service class will start the producer and consumer thread.
import ; import ; /** * @author itmyhome * */ public class ProducerConsumerService { public static void main(String[] args) { // Create BlockingQueue with size 10 BlockingQueue<Mantou> queue = new ArrayBlockingQueue<Mantou>(10); Producer producer = new Producer(queue); Consumer consumer = new Consumer(queue); // Turn on the producer thread to produce messages to the queue new Thread(producer).start(); //Open consumer thread and consume messages in the queue new Thread(consumer).start(); ("Producer and Consumer has been started"); } }
Program running results:
Producer and Consumer has been started
Production of steamed buns: 0
Manufacture of steamed buns: 1
Consumption of steamed buns: 0
Consumption of steamed buns: 1
Production of steamed buns: 2
Consumption of steamed buns: 2
Production of steamed buns: 3
Consumption of steamed buns: 3
Production of steamed buns: 4
Consumption of steamed buns: 4
Production of steamed buns: 5
Consumption of steamed buns: 5
Production of steamed buns: 6
Consumption of steamed buns: 6
......
References
[1]
: The art of concurrent programming in Java[2]
: /tonyspark/p/
This is the article about Java using blocking queue BlockingQueue to implement producers and consumers. For more related content on Java producers and consumers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!