When communicating or signaling between threads, the first thing that comes to mind is java.lang.Object Class method ：wait,notify and notifyAll. This is the most basic and widely accepted concept , But this blog is not about that .
JVM The thread running on the instance lacks a strong default model , As in the UNIX It's the same as interactive communication in , Although there are some third-party frameworks that can help us achieve this intention and work well in the corner case , This blog is especially for the use of Java Three local concepts in to communicate between threads ： Cyclic Barrier Circulation barrier ,Count Down Latch Countdown latch and Pipes The Conduit .
Expand java.util.concurrent.CyclicBarrier Class for data transfer between threads . It's basically a synchronization technology , It allows the program counter of each thread to stop at a common barrier point in each thread , here , You can use shared state to transfer data , Then the thread can continue accordingly .
The demo sample code will use the producer - Consumer model . Parent thread activation class.Producer and class.Consumer Thread start . When a producer reads data from a file , Consumers first reach the public barrier and stop . The producer thread stores the results in the blocking queue of the parent thread . Once the producer reaches the barrier point , At this point, producers and consumers will release barriers , Consumers retrieve data from the queue and continue .
A good use case for this model is that the input of a thread depends on the output of another thread . under these circumstances , The consumer thread can perform all the necessary startup and wait at the barrier to receive input data . According to this github Link to complete implementation of circular barrier .
Logical template ： class.Producer thread step1：start step2： Do the necessary calculations and logic step3： Write all data to the blocking queue step4： Reaching the circulation barrier point , If all threads reach the barrier point - > continue step 5： Carry out cleaning and / Or end class.Consumer thread step1：start step2： Start any logical processing of data independent of the producer step3 Reach the circulation barrier , If all threads reach the barrier - > continue step4： Remove data from the queue step5： Processing data , Clean up and finish
2.Count Down Latch
This is a synchronization technique used in the wait and arrive model , But it's here as a producer - Consumer way to achieve .
The producer thread reached a point , Put on a sign and move on immediately , Unlike the loop barrier, the producer thread will pause here , The consumer thread stops at some point and starts counting the program counter , Wait until the producer thread is marked with a flag before continuing to run .
This model can be modified for inter thread communication . Before reaching this point, the producer thread pushes some data to the blocking queue and raises a flag , Waiting consumers track all of these signs and wait to get the desired sign number , Then it starts to extract data from the blocking queue .
The demo code will have two generator threads , One consumer thread and one size 2 Bounded blocking queue for . Each producer thread reads a different input file , Then transfer it to the producer thread , The producer thread prints it to the screen . A good use case is when database objects in the server are only written in bulk , It acts as the user and waits for the default number . Many producer threads push their data into queues , Raises the flag and continues without waiting . According to this github Link to complete implementation of countdown latch .
Logical template ： class.Producer1 and class.Producer2 step1： start-up step2： Do the necessary calculations and logic step3： Write all the data to be transmitted to the blocking queue step4： Mark and continue without waiting step5： Do some logic and end class.Consumer step1： start-up step2： Do some logic and wait for a point to receive data step3： Tracking signs / Latch , If the quantity reaches - > continue step4： Pull data out of the queue step5 ： Use data and end
3. come from java.io Of Pipes
class.PipedReader and class.PipedWriter Allows direct communication with other threads at defined points . Producers and consumers have to visit a point together , Production starts pushing data in blocks , Consumers waiting for the pipeline to open begin extracting data .
It's relatively easy to understand and implement the concept .pipe Not performing well in the field environment , So it has been abandoned in the developer community . Another major drawback is that it only supports one-to-one transfers . There can only be one producer and one consumer . A good use case is when it's used to guide data in a one-time task , It's related to the newer java.nio Use bags together . Focus on pipeReader and pipeWriter Fully implemented github link .
Logical template class.Producer thread step1： start-up step2： Do some logic and get to the loop of the execution pipeline step3： Push the data into the stream and continue when it's done step4： Do some logic and end class.Consumer thread step1： start-up step2： Do some logic and get to the pipeline reader point step3： Once the pipeline has a value, pull the data and store it step5： Read all the data completely , Do some logic and end
The three ways of communication between heterogeneous threads are only modified when the author completes the work / Efforts to expand the concept of native . As understood in the use case scenario , Countdown latch has a wider range of availability and can be widely used .
One of the main hints is the exception , Provide an obstacle or latch with an underlying exception , The exception handler also has the opportunity to retry the data transfer . These concepts are widely implemented , This blog tries to make producers - Consumer comments and github Examples are put in .