Java Multithread synchronizer code explanation

Synchronizer

Solutions are provided for each specific synchronization problem. Synchronizers are objects that enable threads to wait for another thread and allow them to coordinate actions. The most commonly used synchronizers are countdownlatch and semaphore, and the less commonly used ones are barrier and exchange

Semaphore

Semaphore [semaphore] controls access to shared resources through counters.

Test class:

Thread writing:

Execution result [thread name in [] of all the following output results - output content after]:

It can be seen that after the three semaphores are collected, the subsequent thread will block at the position of receiving the signal. The execution will not continue until a semaphore is released.

CountDownLatch

CountDownLatch [countdown lock], calling countDownLatch.await () in the thread causes the process to enter the blocking state. After the number of times is specified, (countDownLatch.countDown ()), the remaining content in each thread is continued.

A synchronization helper class that allows one or more threads to wait until a set of operations are completed in other threads.

Initializes countdownlatch with the given count. Because the countdown () method is called, the await method is blocked until the current count reaches zero. After that, all waiting threads will be released and all subsequent calls to await will return immediately. This happens only once - the count cannot be reset. If you need to reset the count, consider using cyclicbarrier.

Test class:

Thread class:

Execution results:

CyclicBarrier

Cyclicbarrier [cyclic cycle, circular barrier, barrier] after the number of threads waiting to be blocked in the loop reaches the specified number, the threads participating in the count will continue to execute and can execute specific threads (using different constructors, you can not set execution after arrival), and other threads are still blocked and wait to reach the specified number again.

Test class:

Thread class:

Execution results:

It can be imagined as an irregular long-distance bus station in the past:

Irregular long-distance bus stations will wait until the seats are full before departure, continue to wait after reaching the destination, and then cycle. Everyone is a thread, which triggers the cyclicbarrier after getting on the bus await();, When the specified number of accomplishments is reached, the vehicle departure is the content to be implemented uniformly after the accomplishment. After the departure, people on the vehicle can chat and other operations [we temporarily understand that people can't move o (∩ ∩) O ~] after getting on the vehicle].

Differences between countdownlatch and cyclicbarrier:

Countdownlatch means that one or more threads continue to execute after the count is reached, and the await () call does not participate in the count.

Cyclicbarrier means that n threads wait for each other to execute to the zero boundary point before continuing to execute. Await() calls and participates in counting. Cyclicbarrier supports the execution of an action after the condition is met, and the process is cyclic.

Exchanger

Exchange is used for data exchange between threads

Synchronization point for threads that can pair and exchange elements in pairs. Each thread presents a method on the entry to the exchange method, matches the partner thread, and receives the object of its partner on return. Exchange may be regarded as a two-way form of synchronous queue. Exchange can be useful in applications such as genetic algorithms and pipeline design.

Usage example: the following is a class mainly introduced. This class uses exchange to exchange buffers between threads. Therefore, when necessary, the thread filling the buffer obtains a newly vacated buffer and passes the filled buffer to the thread of the vacated buffer. Test class:

Entity class:

Thread class:

Execution results:

Phaser

Phaser personally feels that it combines the functions of countdownlatch and cyclicbarrier, and provides phased capabilities.

Realize the function of phased cyclicbarrier

Test code:

Thread code:

Execution results:

In the above code, when all threads proceed to arriveandawaitadvance(), the count will be triggered and the threads will be blocked, The equal count is equal to the number of registered threads [that is, when all threads execute to the agreed place, they will be released, so that all threads can continue to execute and trigger onAction events]. We can perform operations of different contents according to different stages in onAction.

Realize the function of countdownlatch in stages

Just change the above test class as follows:

summary

The above is all about the detailed explanation of Java Multithread synchronizer code in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Three methods and examples of Java multithreading interrupt mechanism

On the wonderful use of future in Java multithreading (with source code)

Java understands multithreading by selling tickets

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>