In previous articles, we introduced the tools for concurrent programming in Java: The BlockingQueue interface, ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, and PriorityBlockingQueue are the sixth article in this series.
This article introduces the concurrent programming collection class SynchronousQueue, which implements the BlockingQueue interface. Unlike all BlockingQueue interface implementation classes: SynchronousQueue always has a capacity of 0. This is because SynchronousQueue is not really a queue because it does not maintain storage for its elements. It is simply the medium through which data is exchanged between multiple threads.
SynchronousQueue
An insert from one thread of a queue will always wait for a remove from another thread, and vice versa.put()
Methods that put element objects into the SynchronousQueue do not return a result (blocked) until another thread executestake()
Removes the element object.peek()
Method can get an element in the BlockingQueue interface implementation class, but does not remove it from the queue. This method is not available in SynchronousQueue.
SynchronousQueue Example of a synchronization queue
Let’s write an example to help us understand the features and usage of SynchronousQueue. SynchronousQueueProducer. Java startup thread every two seconds to queue into an Integer object.
public class SynchronousQueueProducer implements Runnable { protected BlockingQueue<Integer> blockingQueue; public SynchronousQueueProducer(BlockingQueue<Integer> queue) { this.blockingQueue = queue; } @Override public void run() { int i = 0; while (true) { System.out.println(Thread.currentThread().getName() + " Put: " + ++i); try { blockingQueue.put(i); Thread.sleep(1000); } catch (InterruptedException e) {e.printStackTrace(); }}}}Copy the code
SynchronousQueueConsumer. Java startup thread every 5 seconds to remove an element from the queue object.
public class SynchronousQueueConsumer implements Runnable { protected BlockingQueue<Integer> blockingQueue; public SynchronousQueueConsumer(BlockingQueue<Integer> queue) { this.blockingQueue = queue; } @Override public void run() { while (true) { try { Integer data = blockingQueue.take(); System.out.println(Thread.currentThread().getName() + " take(): " + data); Thread.sleep(5000); // Consume every 5 seconds} catch (InterruptedException e) {e.printStackTrace(); }}}}Copy the code
SynchronousQueueExample. Java to create a new SynchronousQueue will sync queue, start a producer thread insert objects, two consumer threads to remove the object. Test it out and see how it works.
public class SynchronousQueueExample { public static void main(String[] args) { final BlockingQueue<Integer> synchronousQueue = new SynchronousQueue<>(); SynchronousQueueProducer queueProducer = new SynchronousQueueProducer(synchronousQueue); new Thread(queueProducer).start(); SynchronousQueueConsumer queueConsumer1 = new SynchronousQueueConsumer(synchronousQueue); new Thread(queueConsumer1).start(); SynchronousQueueConsumer queueConsumer2 = new SynchronousQueueConsumer(synchronousQueue); new Thread(queueConsumer2).start(); }}Copy the code
Thread0 is the producer Thread, thread1 and thread2 are the consumer Thread. We can see that SynchronousQueue must produce once, consume once, produce once, consume once, regardless of how many consumer and producer threads must follow this rule.
Thread-0 Put: 1
Thread-1 take(): 1
Thread-0 Put: 2
Thread-2 take(): 2
Thread-0 Put: 3
Thread-1 take(): 3
Thread-0 Put: 4
Thread-2 take(): 4
Thread-0 Put: 5
Thread-1 take(): 5
Thread-0 Put: 6
Thread-2 take(): 6
Thread-0 Put: 7
Thread-1 take(): 7
Thread-0 Put: 8
Thread-2 take(): 8
Thread-0 Put: 9
Thread-1 take(): 9
Thread-0 Put: 10
Thread-2 take(): 10
Thread-0 Put: 11
Thread-1 take(): 11
Thread-0 Put: 12
Thread-2 take(): 12
Copy the code
Application scenario: If we do not know the number of producer requests, but they need to be processed quickly, it is most efficient to allocate one consuming thread for each producer request in conjunction with SynchronousQueue. Executors. NewCachedThreadPool () the underlying use SynchronousQueue will, the thread pool according to demand to create a new thread.
Welcome to my blog, where there are many fine collections
- This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.
Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.
- Spring Boot2.0 by Hand
- Spring Security- JWT-OAUTH2
- RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
- “Actual SpringCloud Micro-service from Bronze to King”
- VUE Series