java CountDownLatch CyclicBarrier

CountDownLatch: 一个或者是一部分线程 ,等待另外一部线程都完成了,再继续执行
       CyclicBarrier: 所有线程互相等待完成。

 

       CyclicBarrier ,一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point)。在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时 CyclicBarrier 很有用。因为该 barrier 在释放等待线程后可以重用,所以称它为循环 的 barrier。

CyclicBarrier可以多次重复使用

 

CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。用给定的计数 初始化 CountDownLatch。由于调用了 countDown() 方法,所以在当前计数到达零之前,await 方法会一直受阻塞。之后,会释放所有等待的线程,await 的所有后续调用都将立即返回。这种现象只出现一次——计数无法被重置。 一个线程(或者多个), 等待另外N个线程完成某个事情之后才能执行,只能使用一次。

 

CountDownLatch主要方法
public CountDownLatch(int count);
public void countDown();
public void await() throws InterruptedException

构造方法参数指定了计数的次数
countDown方法,当前线程调用此方法,则计数减一
awaint方法,调用此方法会一直阻塞当前线程,直到计时器的值为0

 

Java代码

    public class CountDownLatchDemo {  
        final static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        public static void main(String[] args) throws InterruptedException {  
            CountDownLatch latch=new CountDownLatch(2);//两个工人的协作  
            Worker worker1=new Worker("zhang san", 5000, latch);  
            Worker worker2=new Worker("li si", 8000, latch);  
            worker1.start();//  
            worker2.start();//  
            latch.await();//等待所有工人完成工作  
            System.out.println("all work done at "+sdf.format(new Date()));  
        }  
          
        static class Worker extends Thread {  
            String workerName;   
            int workTime;  
            CountDownLatch latch;  
            public Worker(String workerName ,int workTime ,CountDownLatch latch){  
                 this.workerName=workerName;  
                 this.workTime=workTime;  
                 this.latch=latch;  
            }  
            public void run() {  
                System.out.println("Worker "+workerName+" do work begin at "+sdf.format(new Date()));  
                doWork();//工作了  
                System.out.println("Worker "+workerName+" do work complete at "+sdf.format(new Date()));  
            }  
              
            private void doWork() {  
                try {  
                    Thread.sleep(workTime);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  finally {
                    latch.countDown();
                }  
             }
         }

输出:

Worker zhang san do work begin at 2014-10-17 11:05:11
Worker li si do work begin at 2014-10-17 11:05:11
Worker zhang san do work complete at 2014-10-17 11:05:16
Worker li si do work complete at 2014-10-17 11:05:19
all work done at 2014-10-17 11:05:19

 

    package com.study.thread;  
      
    import java.util.Random;  
    import java.util.concurrent.CyclicBarrier;  
    import java.util.concurrent.ExecutorService;  
    import java.util.concurrent.Executors;  
    import java.util.concurrent.TimeUnit;  
      
    /** 
     * 用java模拟4X100接力赛 
     */  
    class Player implements Runnable{  
        private String name;  
        private CyclicBarrier barrier;  
        private Player next;//下一棒  
        private int time;//用时  
        private boolean run;//第一棒  
        public Player(String name, CyclicBarrier barrier, boolean run) {  
            super();  
            this.name = name;  
            this.barrier = barrier;  
            this.run = run;  
        }  
        @Override  
        public void run() {  
            try {  
                synchronized (this) {  
                    while(!run){//等待队员  
                        wait();  
                    }  
                }  
                Random r = new Random();  
                TimeUnit.MILLISECONDS.sleep(r.nextInt(2000));  
                next(next,11 + r.nextInt(2));  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
              
        }  
        private void next(Player next, int time) {  
            System.out.println(name + " 用时:" + time + ",交接棒");  
            if(next != null){  
                next.setTime(this.time + time);  
                synchronized (next) {  
                    next.setRun(true);  
                    next.notify();  
                }  
            }else{  
                System.out.println("跑完,总用时:" + (this.time + time));  
            }  
        }  
        public void setTime(int time) {  
            this.time = time;  
        }  
        public int getTime(){  
            return this.time;  
        }  
        public void setNext(Player next) {  
            this.next = next;  
        }  
        public void setRun(boolean run) {  
            this.run = run;  
        }  
          
    }  
    public class RelayRace {  
          
        public static void main(String[] args) throws InterruptedException {  
            final Player[] players = new Player[4];  
            ExecutorService exec = Executors.newCachedThreadPool();  
            CyclicBarrier barrier = new CyclicBarrier(4, new Runnable() {  
                  
                @Override  
                public void run() {  
                    System.out.println("结束,总用时:" + players[3].getTime());  
                }  
            });  
            for(int i = 0; i < 4; i++){  
                players[i] = new Player("队员" + ( i + 1), barrier, i == 0);  
            }  
            for(int i = 0; i < 4; i++){  
                if( i < 3){  
                    players[i].setNext(players[i + 1]);  
                    exec.execute(players[i]);  
                }else{  
                    exec.execute(players[3]);  
                    break;  
                }  
            }  
            /*TimeUnit.SECONDS.sleep(3); 
             * CyclicBarrier 可以重用 
            for(int i = 0; i < 4; i++){ 
                if( i < 3){ 
                    players[i].setNext(players[i + 1]); 
                    exec.execute(players[i]); 
                }else{ 
                    exec.execute(players[3]); 
                    break; 
                } 
            }*/  
        }  
      
    }  

 输出:

队员1 用时:11,交接棒

队员2 用时:11,交接棒

队员3 用时:11,交接棒

队员4 用时:12,交接棒

跑完,总用时:45

你可能感兴趣的:(CountDownLatch)