CountDownLatch<跑步>

java.util.concurrent
类 CountDownLatch
java.lang.Object
java.util.concurrent.CountDownLatch

--------------------------------------------------------------------------------

public class CountDownLatch extends Object一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。

用给定的计数 初始化 CountDownLatch。由于调用了 countDown() 方法,所以在当前计数到达零之前,await 方法会一直受阻塞。之后,会释放所有等待的线程,await 的所有后续调用都将立即返回。这种现象只出现一次——计数无法被重置。如果需要重置计数,请考虑使用 CyclicBarrier。

CountDownLatch 是一个通用同步工具,它有很多用途。将计数 1 初始化的 CountDownLatch 用作一个简单的开/关锁存器,或入口:在通过调用 countDown() 的线程打开入口前,所有调用 await 的线程都一直在入口处等待。用 N 初始化的 CountDownLatch 可以使一个线程在 N 个线程完成某项操作之前一直等待,或者使其在某项操作完成 N 次之前一直等待。

CountDownLatch 的一个有用特性是,它不要求调用 countDown 方法的线程等到计数到达零时才继续,而在所有线程都能通过之前,它只是阻止任何线程继续通过一个 await。

package com.thread.concurrent;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


class Runner implements Runnable{
    private Random r = new Random();
    private int time;
    private String name;
    private CountDownLatch latch;
    public Runner(String name, CountDownLatch latch){
        this.name = name;
        this.latch = latch;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            time = r.nextInt(10);
            TimeUnit.SECONDS.sleep(time);
            System.out.println("选手:" + name +"已达到终点,耗时" + time + "分钟!");
            latch.countDown();
            
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}
public class CountDownLatchTest02 {
    public static void main(String[] args) {
        ExecutorService es = Executors.newCachedThreadPool();
        final CountDownLatch latch = new CountDownLatch(10);
        //选手
        for(int i = 0; i < 10; i++){
            es.execute(new Runner(Integer.toString(i+1), latch));
        }
        //主线程 阻塞线程
        try {
            latch.await();//等待所有线程结束后继续
            System.out.println("!!!!!!!所有选手都已到达终点!!!!!!!");
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //新线程阻塞
/*        es.execute(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    latch.await();
                    System.out.println("!!!!!!!所有选手都已到达终点!!!!!!!");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        });*/
        
        es.shutdown();
        
    }
}

效果:

选手:2已达到终点,耗时0分钟!
选手:9已达到终点,耗时1分钟!
选手:8已达到终点,耗时3分钟!
选手:3已达到终点,耗时3分钟!
选手:1已达到终点,耗时4分钟!
选手:5已达到终点,耗时4分钟!
选手:4已达到终点,耗时5分钟!
选手:10已达到终点,耗时5分钟!
选手:6已达到终点,耗时6分钟!
选手:7已达到终点,耗时8分钟!
!!!!!!!所有选手都已到达终点!!!!!!!

 

posted @ 2015-11-05 16:41  快帮我摁住你嫂子  阅读(225)  评论(0编辑  收藏  举报