CountDownLatch控制线程执行顺序


package com.dingwang.Thread;

import java.util.concurrent.CountDownLatch;

/**
 * 类FirstThread.java的实现描述:TODO 类实现描述
 * 
 * @author dingwang 2015年10月13日 下午4:01:06
 */
public class ThirdThread implements Runnable {

    private final CountDownLatch latch;

    public ThirdThread(CountDownLatch latch) {
        this.latch = latch;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        System.out.println("the third thread is running!");
        //计算器-1
        latch.countDown();
        System.out.println("ThirdThread----------->count=" + latch.getCount());
    }

    public static void main(String[] args) throws InterruptedException {
        //FirstThread,SecondThread与ThirdThread实现完全相同
        //构造线程计数器
        final CountDownLatch latch = new CountDownLatch(3);
        Thread t1 = new Thread(new FirstThread(latch));
        Thread t2 = new Thread(new SecondThread(latch));
        Thread t3 = new Thread(new ThirdThread(latch));
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
        //线程计数器技术为0时,继续执行主线程
        latch.await();
        System.out.println("the main thread is End...");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值