码迷,mamicode.com
首页 > 其他好文 > 详细

connectedSignal 简单使用

时间:2014-09-27 13:10:19      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:blog   io   使用   ar   java   for   sp   div   art   

import java.util.concurrent.CountDownLatch;

public class CountDown {
	private static CountDownLatch connectedSignal = new CountDownLatch(50);

	static class MyThread implements Runnable {

		public void run() {
			for (int i = 0; i < 25; ++i) { sale(); }
		}

		public synchronized void sale() {
			if (connectedSignal.getCount() > 0) {
				Thread current = Thread.currentThread();
				connectedSignal.countDown();
				System.out.println("Thread id" + current.getId() + "value = "
						+ connectedSignal.getCount());
			}
		}

	}

	public static void main(String[] args) throws InterruptedException {
		MyThread my = new MyThread();
		Thread t1 = new Thread(my, "test1");
		Thread t2 = new Thread(my, "test2");
		
        t1.start();
        t2.start();
        
        connectedSignal.await(); // 等待结束
        System.out.println(connectedSignal.getCount()); 
	}
}

 connectedSignal

connectedSignal 简单使用

标签:blog   io   使用   ar   java   for   sp   div   art   

原文地址:http://www.cnblogs.com/chengxin1982/p/3996084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!