Use of cyclicbarrier for Java Concurrent instances

Recently, I've been integrating concurrency. By the way, I'll write some examples of Java concurrency to share with you and strengthen my memory. If there are any mistakes or improper places, you are welcome to correct them.

Cyclicbarrier is a multi-threaded concurrency control utility, which is very similar to countdownlatch. It can also count and wait between threads, but its function is more complex and powerful than countdownlatch.

Introduction to cyclicbarrier

Cyclicbarrier literally means a barrier that can be used cyclically. What it needs to do is to block a group of threads when they reach a barrier (also known as synchronization point). The barrier will not open until the last thread reaches the barrier, and all threads intercepted by the barrier will continue to work. Threads enter the barrier and pass through cyclicbarrier's await() method.

The default construction method of cyclicbarrier is cyclicbarrier (int parties). Its parameter indicates the number of threads intercepted by the barrier. Each thread calls the await method to tell cyclicbarrier that I have reached the barrier, and then the current thread is blocked.

Cyclicbarrier also provides a more advanced constructor cyclicbarrier (int parties, runnable barrieraction), which is used to preferentially execute the runnable object barrieraction when the thread reaches the barrier, so as to facilitate the processing of more complex business scenarios.

Implementation principle: a lock object is defined inside the cyclicbarrier. Whenever a thread calls the await method of the cyclicbarrier, the number of remaining intercepted threads is reduced by 1, and then judge whether the number of remaining intercepted threads is 0. If not, enter the condition queue of the lock object and wait. If yes, execute the runnable method of the barrieraction object, and then put all threads in the lock condition queue into the lock waiting queue. These threads will successively acquire and release the lock, and then return from the await method first, and then from the await method of cyclicbarrier.

Cyclicbarrier is mainly used to wait for each other between a group of threads, while countdownlatch is generally used to wait for another group of threads. In fact, the cyclicbarrier function can be realized through countdown() and await() of countdownlatch. That is, countdown() + await() in countdownlatch = await() in cyclicbarrier. Note: call countDown () first in a thread and then call await ().

The constructor cyclicbarrier can be understood as a circular fence, and this counter can be used repeatedly. For example, if we set the counter to 10, the counter will return to zero after collecting the first batch of 10 threads, and then collect the next batch of 10 threads, which is its internal meaning.

There are many players in lol and the glory of the king. Many people should have the experience of playing the dragon. In the early stage, everyone plans to steal the Dragon together. Because everyone is weak in the early stage, it takes five people to play the dragon. How can this program be realized? I'm very good. At first, my code was written like this (haha, don't worry about my time):

The results are as follows:

After that, everyone spent 40s on the road, which is obviously wrong. If you do that, they'll steal all your towers. No, I have to improve. How to change it? Multithreading concurrent execution. If I change it to the following, use the volatile keyword.

The results are as follows:

The result seems good, but it's a little troublesome to deal with. You need while (I! = 0) to cycle all the time. At this time, I learned to use cyclicbarrier to process. The code is as follows:

Everyone waited before they arrived. The results are as follows:

Cyclicbarrier is equivalent to the counter of the thread:

Specify a number during cyclicbarrier initialization, and then calculate that cyclicbarrier has been called The number of threads await() entered to wait. When the number of threads reaches this number, all threads entering the waiting state are awakened and continue.

Cyclicbarrier, as its name means, can be regarded as an obstacle. All threads must be assembled before they can pass through this obstacle together.

Cyclicbarrier can also take a runnable parameter initially. This runnable task is executed after the number of cyclicbarriers reaches and before all other threads are awakened.

Of course, there is no difference between using cyclicbarrier and using countdownlatch. As mentioned earlier, the functions of cyclicbarrier are more complex and powerful. Let me show you an example from the book "practical Java high concurrency programming".

For example, the commander issued an order asking 10 soldiers to complete a task together. At this time, 10 soldiers will be asked to gather and report first, and then go to the task bravely. When all 10 soldiers have completed the task, the driver can declare that the task is completed. Compared with countdownlatch, cyclicbarrier can accept a parameter as barrieraction. The so-called barrieraction is the action that the system will execute when the counter finishes counting once. The following constructor, where parties represents the total number of technologies, that is, the total number of threads participating.

public CyclicBarrier(int parties,Runnable barrierAction)

The following example demonstrates the above task scenario

The results are as follows:

summary

The above is all about the use of cyclicbarrier of Java Concurrent instances in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Java Web applications use stream limiting to handle a large number of concurrent requests

Implementation of BlockingQueue for Java Concurrent learning

Detailed explanation of high concurrency solution of Java System

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
分享
二维码
< <上一篇
下一篇>>