Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to analyze concurrent tool classes CountDownLatch and CyclicBarrier

Shulou Source: shulou.com Published: 2022-06-01 06:55:26 09月22日 Update

How to carry out concurrent tool class CountDownLatch and CyclicBarrier analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

CountDownLatch

The basic process is that a thread waits for another thread to finish its work before executing. Enhanced join

Class, await is used to wait, and countDown (in the thread) is used to subtract one.

It is up to the initialization thread to decide whether to let it go.

Let's demonstrate the basic use of CountDownLatch in code:

Public class UserCountDownLatch {static CountDownLatch latch = new CountDownLatch (6); / / initialize thread private static class InitThread implements Runnable {@ Override public void run () {System.out.println ("Thread_" + Thread.currentThread (). GetId () + "ready init work..."); latch.countDown (); / / initialize thread finished working. Deduct once}} public static void main (String [] args) throws InterruptedException {/ / separate initialization thread. Initialization is divided into 2 steps, and new Thread (new Runnable ()) {@ Override public void run () {try {Thread.sleep (1000) is deducted twice. System.out.println ("Thread_" + Thread.currentThread (). GetId () + "ready init work step lst."); latch.countDown (); / / deduction of System.out.println ("begin step 2nd.") for each step completed; Thread.sleep (1000) System.out.println ("Thread_" + Thread.currentThread (). GetId () + "ready init work step 2nd."); latch.countDown (); / / deduction for each step completed} catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}

}}). Start (); for (int item0)

Tags: Thread work barrier open action implementer difference code summary method result help demonstration run block tool analysis clarity content function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia Microsoft Shulou Information macOS