Java Concurrent Programming: detailed examples of countdownlatch, cyclicbarrier and semaphore

Java Concurrent Programming: detailed examples of countdownlatch, cyclicbarrier and semaphore

In Java 1.5, some very useful auxiliary classes are provided to help us with concurrent programming, such as countdownlatch, cyclicbarrier and semaphore. Today we will learn the usage of these three auxiliary classes.

The following is the outline of this article:

I Countdownlatch usage 2 Cyclicbarrier usage III Semaphore usage

If there is anything wrong, please forgive me and welcome criticism and correction.

I Countdownlatch usage

The countdownlatch class is located in Java util. Under the concurrent package, it can realize functions similar to counters. For example, there is a task a, which can only be executed after the other four tasks are executed. At this time, countdownlatch can be used to realize this function.

The countdownlatch class provides only one constructor:

Then, the following three methods are the most important methods in the countdownlatch class:

Let's take a look at an example to understand the usage of countdownlatch:

Execution results:

II Cyclicbarrier usage

Literally, loop fence, through which a group of threads can wait to a certain state and then execute all at the same time. It is called loopback because the cyclicbarrier can be reused after all waiting threads are released. Let's call this state barrier for the moment. When await () method is called, the thread will be in barrier.

The cyclicbarrier class is located in Java util. Under the concurrent package, cyclicbarrier provides two constructors:

The parameter parties refers to the number of threads or tasks to wait to the barrier state; The parameter barrieraction is the content that will be executed when these threads reach the barrier state.

Then, the most important method in cyclicbarrier is the await method, which has two overloaded versions:

The first version is commonly used to suspend the current thread until all threads reach the barrier state, and then execute subsequent tasks at the same time;

The second version is to let these threads wait for a certain time. If there are still threads that do not reach the barrier state, they will directly let the threads that arrive at the barrier execute subsequent tasks.

Here are a few examples:

If several threads have to write data, and only after all threads have completed the write data operation, can these threads continue to do the following things. At this time, you can use the cyclicbarrier:

Execution results:

It can be seen from the above output results that after each write thread completes the write data operation, it is waiting for other threads to complete the write operation.

When all threads finish writing, all threads continue to perform subsequent operations.

If you want to perform additional operations after all threads are written, you can provide the runnable parameter for the cyclicbarrier:

Operation results:

It can be seen from the results that when all four threads reach the barrier state, one of the four threads will be selected to execute runnable.

Let's take a look at the effect of specifying a time for await:

Execution results:

The above code deliberately delays the start of the last thread in the for loop of the main method, because after the first three threads have reached the barrier, they wait for the specified time and find that the fourth thread has not reached the barrier, so they throw an exception and continue to execute the following tasks.

In addition, cyclicbarrier can be reused. See the following example:

Execution results:

It can be seen from the execution results that after the first four threads cross the barrier state, they can be used for a new round of use. Countdownlatch cannot be reused.

III Semaphore usage

Semaphore literally means semaphore. Semaphore can control the number of threads accessed at the same time, obtain a license through acquire(), wait if not, and release() releases a license.

The semaphore class is located in Java util. Under the concurrent package, it provides two constructors:

Here are some important methods in the semaphore class. The first is the acquire() and release() methods:

Acquire () is used to obtain a license. If no license can be obtained, it will wait until the license is obtained.

Release() is used to release the license. Note that permission must be obtained before release.

These four methods will be blocked. If you want to get the execution result immediately, you can use the following methods:

You can also get the number of licenses available through the availablepermissions () method.

Let's take an example to see the specific use of semaphore:

If a factory has five machines but eight workers, one machine can only be used by one worker at the same time. Only when it is used up can other workers continue to use it. Then we can realize it through semaphore:

Execution results:

The following is a summary of the three auxiliary classes mentioned above:

1) Countdownlatch and cyclicbarrier can realize the waiting between threads, but they have different emphases:

Countdownlatch is generally used for a thread a to wait for several other threads to execute tasks before it executes; Cyclicbarrier is generally used for a group of threads waiting for each other to a certain state, and then this group of threads execute at the same time; In addition, countdownlatch cannot be reused, while cyclicbarrier can be reused.

2) Semaphore is actually a bit similar to a lock. It is generally used to control access to a group of resources.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

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