CyclicBarrier and CountDownLatch difference

CyclicBarrier and CountDownLatch are located under the package java.util.concurrent

A, CountDownLatch usage

CountDownLatch class only provides a constructor:

public CountDownLatch (int count) {} ; // count value of the count parameter is
then the following three methods are the most important class CountDownLatch Method:

public void await () throws InterruptedException { }; // thread calls the await () method will be suspended, it waits until the count value of zero before continuing execution
public boolean await (long timeout, TimeUnit unit) throws InterruptedException {}; // and the await () is similar, except to wait for a certain time after the count value becomes 0, then it will not proceed to
public void countDown () {}; // the count value by one


A CountDownLatch, a synchronization aid, until a set of operations is performed in other threads, which allows one or more threads to wait.

下面举个例子说明:
package main.java.CountDownLatch;
 
import java.util.concurrent.CountDownLatch;
 
/**
 * PROJECT_NAME:downLoad
 * Author:lucaifang
 * Date:2016/3/18
 */
public class countDownlatchTest {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        for(int i=0;i<5;i++){
            new Thread(new readNum(i,countDownLatch)).start();
        }
        countDownLatch.await();
        System.out.println("线程执行结束。。。。");
    }
 
    static class readNum  implements Runnable{
        private int id;
        a CountDownLatch LATCH Private;
        public readNum (int ID, a CountDownLatch LATCH) {
            this.id ID =;
            this.latch = LATCH;
        }
        @Override
        public void RUN () {
            the synchronized (the this) {
                System.out.println ( "ID:" the above mentioned id +);
                latch.countDown ();
                System.out.println ( "thread group task" + id + "end, other tasks continue");
            }
        }
    }
}

Output:
the above mentioned id: 1
thread group Task 1 over, other tasks continue
id: 0
thread group task 0 ends, the other tasks continue
id: 2
Thread Group Task 2 over, other tasks continue
id: 3
thread group task 3 over, other tasks continue
id: 4
end task 4 thread group, other tasks continue
thread execution ends. . . .

Thread countDown () after, will continue to perform its task, but in the end all will CyclicBarrier threaded tasks, will carry out follow-up tasks, specific examples can be seen below.

Two, CyclicBarrier usage

CyclicBarrier provides two constructors:

CyclicBarrier public (int parties, Runnable barrierAction) {
}
 
public CyclicBarrier (int parties) {
}
parameter parties refers to how many threads to make or tasks waiting to barrier state; parameter barrierAction content when these threads have reached the barrier state will perform.
CyclicBarrier The most important way is to await method

public int await () throws InterruptedException, BrokenBarrierException {}; // suspends the current thread, until all threads have reached the barrier state again while performing subsequent task;
public int the await (Long timeout, TimeUnit Unit) throws InterruptedException, a BrokenBarrierException, a TimeoutException {} ; // let these threads to wait until a certain time, if there is no thread to reach the barrier state to directly thread reaches the barrier to perform follow-up tasks


举例说明
package main.java.countOff;
 
import java.util.concurrent.CyclicBarrier;
 
/**
 * PROJECT_NAME:downLoad
 * Author:lucaifang
 * Date:2016/3/18
 */
public class cyclicBarrierTest {
    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {
            @Override
            public void run() {
                System.out.println("线程组执行结束");
            }
        });
        for (int i = 0; i < 5; i++) {
            new Thread(new readNum(i,cyclicBarrier)).start();
        }
        //CyclicBarrier 可以重复利用,
        // 这个是CountDownLatch做不到的
//        for (int i = 11; i < 16; i++) {
//            new Thread(new readNum(i,cyclicBarrier)).start();
//        }
    }
    static class readNum  implements Runnable{
        private int id;
        private CyclicBarrier cyc;
        public readNum(int id,CyclicBarrier cyc){
            this.id = id;
            this.cyc = cyc;
        }
        @Override
        public void run() {
            synchronized (this){
                System.out.println("id:"+id);
                try {
                    cyc.await();
                    System.out.println ( "thread group task" + id + "end, continue other tasks");
                } the catch (Exception E) {
                    e.printStackTrace ();
                }
            }
        }
    }
}

Output:
the above mentioned id: 1
the above mentioned id: 2
the above mentioned id: 4
the above mentioned id: 0
the above mentioned id: 3
thread group to perform the end of
the thread group task 3 over, other tasks continue
the thread group Task 1 over, other tasks continue to
the end of the thread group Task 4, other tasks continue
thread 0 end group task, other tasks continue to
thread groups task 2 over, other tasks continue

Published 79 original articles · won praise 48 · views 90000 +

Guess you like

Origin blog.csdn.net/bianyamei/article/details/100163631