Concurrency control class CountDownLatch

 

CountDownLatch is a synchronization tool class that allows one or more threads to wait until the operations of other threads are completed and then execute;

CountDownLatch was introduced in java1.5 and exists under the java.util.concurrent package. The CountDownLatch class enables a thread to wait for other threads to complete their work before executing. For example, the main thread of the application may want to execute after the thread responsible for starting the framework services has started all the framework services.

CountDownLatch is implemented by a counter whose initial value is the number of threads. Every time a thread completes its task, the value of the counter is decremented by 1. When the counter value reaches 0, it means that all threads have completed the task, and then the thread waiting on the latch can resume executing the task;

The CountDownLatch class has 3 basic elements:

  1. The initial value determines the number of events the CountDownLatch class needs to wait for.
  2. await() method, called by threads waiting for all events to terminate.
  3. countDown() method, which is called after the event finishes execution.

When a CountDownLatch object is created, the object uses the constructor's parameters to initialize an internal counter. Each time the countDown() method is called, the internal counter of the CountDownLatch object is decremented by one. When the internal counter reaches 0, the CountDownLatch object wakes up all the threads sleeping using the await() method.

It is not possible to reinitialize or modify the value of the CountDownLatch object's internal counter. Once the value of the counter is initialized, the only way to modify it is the countDown() method used previously. When the counter reaches 0, all calls to the await() method will return immediately, and any subsequent calls to the countDown() method will have no effect.

example:

   

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(10);
        final CounterDemo1 counterDemo = new CounterDemo1();
        int callTime = 100000;
        final String url = "http://localhost:8080/hello";
        CountDownLatch countDownLatch = new CountDownLatch(callTime);
        //Simulate interface call statistics under concurrent conditions
        for(int i=0;i<callTime;i++){
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    counterDemo.increase(url);
                    countDownLatch.countDown();
                }
            });
        }
        try {
           countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
        executor.shutdown();
        //Wait for the number of calls to be output after all threads are counted
        System.out.println("Number of calls: "+counterDemo.getCount(url));

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326986391&siteId=291194637