Art Java concurrent programming (14) CountDownLatch

CountDownLatch

CountDownLatch allows one or more threads waiting for other threads to finish

If there is such a demand: we need to parse an Excel sheet where more data, then you can consider using multiple threads, each parse the data in a sheet, wait until after the completion of all of the sheet are resolved, the program requires prompt completion of the analysis. In this demand in order to achieve the main thread waits for all threads to complete the parsing operation sheet, when using the easiest way to join () method

public class JoinCountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        Thread parser1 = new Thread(new Runnable() {
            @Override
            public void run() {
            }
        });
        Thread parser2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("parser2 finish");
            }
        });
        parser1.start();
        parser2.start();
        parser1.join();    join用于让当前执行线程等待join线程执行结束。其实现原理是不停检查join线程是否存活,如果join线程存活则让当前线程永远等待。
        parser2.join();
        System.out.println("all parser finish");
    }
}
        while (isAlive()) {
            wait(0);          永远等待下去
        }

Knowing join thread terminates, the thread this.notifyAll () method is called, the call notifyAll () method is readily available in the JVM.

JDK1.5 and contract provides CountDownLatch can also be achieved join the function, and the function of more than join

public class CountDownLatchTest {
	CountDownLatch的构造函数接收一个int类型的参数作为计数器,如果你想等待N个点完成,这里就传n
    static CountDownLatch c = new CountDownLatch(2);
    public static void main(String[] args) throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(1);
                c.countDown();       调用此方法n就会减1  
                System.out.println(2);
                c.countDown();        由于countDown()可以用到任何地方,所以这里说N个点,可以是n个线程,也可以是1个线程里的n个步骤。用在多个线程时,只需要把这个CountDownLatch的引用传递到线程里即可
            }
        }).start();
        c.await();             会阻塞当前线程,直到n变成0.
        System.out.println("3");
    }
}

If there is a parsing sheet threading more slowly, we could not get the main thread has been waiting for, so you can use await another method with the specified time --await (long time, TimeUnit unit), this method waits for a specific time it will no longer block the current thread. join has a similar approach.

Counter must be greater than or equal to 0, just equal to 0 when the counter is zero, do not block the current thread calls await method. CountDownLatch impossible to re-initialize or modify the value of the internal counter CountDownLatch object. A thread calls countDown method happen-before, another thread calls await method.

Published 24 original articles · won praise 1 · views 537

Guess you like

Origin blog.csdn.net/qq_45366515/article/details/105266397