菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

  延迟线程的进度,等待闭锁条件的结束

import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.concurrent.CountDownLatch;

public class Main
{

	final static CountDownLatch start = new CountDownLatch(1);
	final static CountDownLatch end = new CountDownLatch(5);

	public static void main(String[] args) throws FileNotFoundException,
			NoSuchAlgorithmException, ParseException, InterruptedException
	{
		long t1 = System.currentTimeMillis();
		for (int i = 0; i < 5; i++)
		{
			new Thread(new MyRunable(i)).start();
		}
		start.countDown();
		end.await();
		// long total = Integer.MAX_VALUE*5;
		// for(long i = 0; i < total;i++);
		long t2 = System.currentTimeMillis();
		System.out.println("total time=" + (t2 - t1));
		System.out.println("total time=" + (t2 - t1) / 1000);
	}

	static class MyRunable implements Runnable
	{
		int i;

		public MyRunable(int i)
		{
			this.i = i;
		}

		public void run()
		{
			try
			{
				start.await();
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			for (int i = 0; i < Integer.MAX_VALUE; i++)
				if (i == 100)
				{
					System.out.println(i + " is 100");
				}
			System.out.println(i + " is end");
			end.countDown();
		}
	}
}

  

posted on 2017-05-11 16:44  好吧,就是菜菜  阅读(277)  评论(0编辑  收藏  举报