CountDownLatch应用实战

1. CountDownLatch简介java

       一个同步辅助类,在完成一组正在其余线程中执行的操做以前,它容许一个或多个线程一直等待。其本质就是一个共享锁。dom

       他最主要的做用是用来同步java的线程。ide

主要有如下三个方法:this

主要方法:spa

 public CountDownLatch(int count);
 public void countDown();
 public void await();

构造方法参数指定了计数的次数线程

countDown方法,当前线程调用此方法,则计数减一code

await方法,调用此方法会一直阻塞当前线程,直到计时器的值为0同步

2. 实战应用的例子it

         一个模拟运动员比赛的例子,首先要等全部的运动员所有给准备好,而后发起“起跑指令”全部的运动员开始起跑,等全部的运动员跑完才结束本次笔试。io

代码以下:

运动员线程代码:

package it_cast.day01;
import java.util.concurrent.CountDownLatch;
public class Player implements Runnable {
    private int id;
    private CountDownLatch begin;
    private CountDownLatch end;
    
    public Player(int id, CountDownLatch begin, CountDownLatch end) {
        this.id=id;
        this.begin=begin;
        this.end=end;
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
                System.out.println("Play" + id + "ready");
                begin.await();// 全部的运动员在这准备者,当一声令下就开始跑
                Thread.sleep((long) (Math.random() * 100));// 随机分配时间,即运动员完成时间
                System.out.println("Play" + id + " arrived.");
            } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            end.countDown();// 每一个运动员跑完,使end状态减1,最终减至0
        }        
    }
}

主代码:

package it_cast.day01;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDownLatchDemo {
    private static final int PLAYER_AMOUNT = 5;
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        //对于每位运动员,CountDownLatch减1后即结束比赛
        CountDownLatch begin = new CountDownLatch(1);
        
        //对于整个比赛,全部运动员结束后才算结束
        CountDownLatch end = new CountDownLatch(PLAYER_AMOUNT);
        Player [] plays=new Player[PLAYER_AMOUNT];
        
        //初始化5个运动员
        for (int i = 0; i < plays.length; i++) {
            plays[i]=new Player(i, begin, end);
        }
        
        //设置特定的线程池,大小为5
        ExecutorService exe = Executors.newFixedThreadPool(PLAYER_AMOUNT);
        for(Player p:plays)
            exe.execute(p); 
        Thread.sleep(1000);
        System.out.println("Race begins!");
        begin.countDown();
        end.await();; //等待end状态变为0,即为全部人都跑完了,即为比赛结束
        System.out.println("Race ends!");
        exe.shutdown();     
    }
}

运行结果以下:

相关文章
相关标签/搜索