A tool class for thread collaboration to control concurrent processes

** What is controlling concurrent flow? 支那

The utility class for controlling concurrent processes, and what it does is it helps us programmers make it easier for threads to cooperate with each other, to cooperate with each other, to satisfy the business logic of having thread A wait until thread B has finished executing and so on

CountDownLatch

The bottom door:

Examples: shopping groups; The bus doesn’t leave until it’s full

Process: The thread waits until the countdown is over, and then continues to work until the countdown is over. Start -> Enter wait -> Countdown ends -> Continue working

Class main methods:

CountDownLatch(int Count) : There is only one constructor that takes count to the value that needs to be reciprocal

Await () : The thread calling the await() method is suspended and waits until the count value is 0 before continuing. CountDown () : Subtract the count value by 1 until it reaches 0, waiting threads will not be aroused.

Diagram await and countDown methods:

Constructor code:

Note:

Extended usage: multiple threads and other threads have completed the execution, and then execute simultaneously

CountDownLatch is not reusable, so if you need a recount, consider using CyclicBarrier or creating a new instance of CountDownLatch

A Semaphore is a Semaphore

Semaphore can be used to restrict or management of a limited number of resources usage Semaphore’s role is to maintain a license counts, thread can get license, the signal of the rest of the license plus one, when the license number is 0, the Semaphore have also want to get a license under the name of the thread, just need to wait for, Knowing that another thread has released the license

Semaphore usage:

Initialize Semaphore and specify the number of licenses

Add a acquire() or acquireUninterruptibly() method in front of the code that needs to be threaded

At the end of the task, release() is called to release the license

Semaphore main methods:

  • Acquire () : acquire a license, which can be interrupted
  • AcquireUninterruptibly () : Obtains the license without interruption
  • New Semaphore(int permits, Boolean fair) : If true is passed, nameSemaphore will put the threads that have been waiting in the FIFO queue so that when a new license is available, it can be distributed to the threads that have been waiting the longest
  • TryAcquire () : See if there are any available permits, if there are, get them, if not, do something else.
  • TryAcquire (timeout) : Same as tryAcquire, but with an extra timeout, e.g. if you don’t get the license within 3 seconds, do something else.

Constructor code:

Note:

The number of licenses obtained and released must be the same. Otherwise, for example, if you acquire 2 licenses at a time and only release 1 license at a time, over time, the number of licenses will be insufficient, resulting in four programs.

Note that fairness is set when initializing Semaphore. It is usually more appropriate to set fairness to true.

It is not necessary for the thread that acquired the license to release the license. In fact, there is no requirement for the thread to acquire and release the license. It may be that A acquires the license and B releases it, as long as it makes sense

For example, if thread 1 needs thread 2 to complete its preparation before it can execute, then thread 1acquire, and thread 2 releases after its execution. In this case, if thread 1 needs thread 2 to complete its preparation before it can execute, then thread 1acquire and thread 2 releases after its execution. It’s like a lightweight CountDownLatch

Iv. Condition interface (also called Condition object)

Condition role

  • When thread 1 needs to wait for a condition, it executes the condition.await() method, and once it executes the await() method, the thread is blocked.
  • Then, normally, another thread, let’s say thread 2, executes the condition, and when that condition is reached, thread 2 executes condition.signal(), and the JVM finds the threads that are waiting on the condition from the blocked threads. When thread 1 receives the executable message, its thread state becomes Runnable

SignalAll () is different from signal()

  • SignalAll () : Calls all waiting threads
  • Signal: Only the thread with the longest wait time will be aroused

Constructor code:

Note:

  • In fact, if lock is used instead of synchronized, then Condition is used instead of Object. Wait /notify, so the usage and nature are almost the same
  • Await method automatically releases the lock it holds. Like object. wait, it does not need to release the lock itself manually
  • When calling await, the lock must be held or an exception will be thrown

CyclicBarrier

  • Cyclicbarriers are similar to CountdownLatches in that they block a set of threads
  • CyclicBarrier is used when there are a number of threads working together to compute different tasks and a final summary is needed. CyclicBarrier can construct a rally point. When a thread finishes executing, it will wait at the rally point until all threads have reached the rally point. Then the barrier will be removed and all threads will start out in unity to continue the rest of the task.

Code demo:

CyclicBarrier and CountDownLatch

  • The CyclicBarrier waits for a fixed number of threads to reach the barrier position before executing. CountDownLatch only waits for the number to reach 0. That is, CountDownLatch is used for time, but CyclicBarrier is used for threads
  • Reusability is different: CountDownLatch, after counting down to 0 and triggering the latch to open, cannot be used again unless a new instance is created; CyclicBarrier can be reused.