如何尽可能地使10个线程同时开始工作

在多线程编程中,我们很难控制线程的具体启动时间,调用线程的Start()只是启动一个线程,使其处于就绪阶段,至于什么时候CPU开始执行线程Run方法,这是操作系统调度决定的,在理论上,如果有多个CPU,操作系统的调度可能使的多个线程真正同时开始,但是在单CPU的机器上,是没法做到多个线程真正同时启动的。我们只能尽可能地使得线程之间的启动间隔很短,模拟多个线程同时启动。下面这个程序模拟一个有10辆赛车参加的赛车比赛,通过使用CountDownLatch来控制10辆赛车尽可能地同时启动。

public class StartSimultaneously {
	
	private CountDownLatch startGate ;
	private CountDownLatch endGate ;
	
	private int count;
	
	public StartSimultaneously(int count)
	{
		this.count = count;
		this.startGate = new CountDownLatch(1);
		this.endGate = new CountDownLatch(count);
	}
	
	private class racingThread extends Thread
	{
		public void run()
		{
			try {
				startGate.await();
				System.out.println("Thread "+currentThread().getId()+ " start's time is :" + System.currentTimeMillis());
				Thread.sleep(6000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally
			{
				endGate.countDown();
				System.out.println("Thread "+currentThread().getId()+ " end's time is :" + System.currentTimeMillis());
			}
		}
	}
	
	public void start() throws InterruptedException
	{
		for(int i = 0; i < count; i++)
		{
			new racingThread().start();
		}
		
		System.out.println("Thread "+Thread.currentThread().getId()+ " start's time is :" + System.currentTimeMillis());
		startGate.countDown();
		endGate.await();
		System.out.println("Thread "+Thread.currentThread().getId()+ " end's time is :" + System.currentTimeMillis());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			new StartSimultaneously(10).start();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  该程序的执行结果为:

Thread 1 start's time is :1304690067375
Thread 9 start's time is :1304690067375
Thread 8 start's time is :1304690067375
Thread 12 start's time is :1304690067375
Thread 15 start's time is :1304690067375
Thread 10 start's time is :1304690067375
Thread 17 start's time is :1304690067375
Thread 16 start's time is :1304690067375
Thread 13 start's time is :1304690067375
Thread 14 start's time is :1304690067375
Thread 11 start's time is :1304690067375
Thread 9 end's time is :1304690073375
Thread 10 end's time is :1304690073375
Thread 8 end's time is :1304690073375
Thread 12 end's time is :1304690073375
Thread 16 end's time is :1304690073375
Thread 14 end's time is :1304690073375
Thread 15 end's time is :1304690073375
Thread 17 end's time is :1304690073375
Thread 13 end's time is :1304690073375
Thread 11 end's time is :1304690073375
Thread 1 end's time is :1304690073375

 但是正如上面所说,这只是尽可能地使线程同时启动,随着代码中的count值的增大,各个线程的启动时间的偏差也越来越大,如果count为100,则执行的结果为:

Thread 1 start's time is :1304690372640
Thread 9 start's time is :1304690372640
Thread 8 start's time is :1304690372640
Thread 10 start's time is :1304690372640
Thread 12 start's time is :1304690372640
Thread 11 start's time is :1304690372640
Thread 13 start's time is :1304690372640
Thread 18 start's time is :1304690372640
Thread 19 start's time is :1304690372640
Thread 14 start's time is :1304690372640
Thread 42 start's time is :1304690372656
Thread 46 start's time is :1304690372656
Thread 47 start's time is :1304690372656
Thread 49 start's time is :1304690372656
Thread 51 start's time is :1304690372656
Thread 53 start's time is :1304690372656
Thread 54 start's time is :1304690372656
Thread 61 start's time is :1304690372656
Thread 67 start's time is :1304690372656
Thread 69 start's time is :1304690372656
Thread 71 start's time is :1304690372656
Thread 73 start's time is :1304690372656
Thread 75 start's time is :1304690372656
Thread 77 start's time is :1304690372656
Thread 74 start's time is :1304690372656
Thread 76 start's time is :1304690372656
Thread 82 start's time is :1304690372656
Thread 68 start's time is :1304690372656
Thread 16 start's time is :1304690372640
Thread 15 start's time is :1304690372640
Thread 102 start's time is :1304690372656
Thread 103 start's time is :1304690372656
Thread 96 start's time is :1304690372656
Thread 99 start's time is :1304690372671
Thread 44 start's time is :1304690372656
Thread 100 start's time is :1304690372671
Thread 101 start's time is :1304690372671
Thread 45 start's time is :1304690372656
Thread 104 start's time is :1304690372656
Thread 105 start's time is :1304690372656
Thread 97 start's time is :1304690372656
Thread 106 start's time is :1304690372656
Thread 107 start's time is :1304690372656
Thread 43 start's time is :1304690372656
Thread 94 start's time is :1304690372656
Thread 93 start's time is :1304690372656
Thread 95 start's time is :1304690372656
Thread 92 start's time is :1304690372656
Thread 89 start's time is :1304690372656
Thread 91 start's time is :1304690372656
Thread 87 start's time is :1304690372656
Thread 90 start's time is :1304690372656
Thread 88 start's time is :1304690372656
Thread 84 start's time is :1304690372656
Thread 86 start's time is :1304690372656
Thread 85 start's time is :1304690372656
Thread 80 start's time is :1304690372656
Thread 83 start's time is :1304690372656
Thread 78 start's time is :1304690372656
Thread 70 start's time is :1304690372656
Thread 72 start's time is :1304690372656
Thread 81 start's time is :1304690372656
Thread 79 start's time is :1304690372656
Thread 66 start's time is :1304690372656
Thread 64 start's time is :1304690372656
Thread 65 start's time is :1304690372656
Thread 63 start's time is :1304690372656
Thread 62 start's time is :1304690372656
Thread 60 start's time is :1304690372656
Thread 59 start's time is :1304690372656
Thread 58 start's time is :1304690372656
Thread 57 start's time is :1304690372656
Thread 56 start's time is :1304690372656
Thread 55 start's time is :1304690372656
Thread 52 start's time is :1304690372656
Thread 50 start's time is :1304690372656
Thread 48 start's time is :1304690372656
Thread 28 start's time is :1304690372640
Thread 27 start's time is :1304690372640
Thread 41 start's time is :1304690372656
Thread 40 start's time is :1304690372656
Thread 34 start's time is :1304690372656
Thread 39 start's time is :1304690372656
Thread 38 start's time is :1304690372656
Thread 37 start's time is :1304690372656
Thread 36 start's time is :1304690372656
Thread 35 start's time is :1304690372656
Thread 26 start's time is :1304690372640
Thread 33 start's time is :1304690372656
Thread 32 start's time is :1304690372656
Thread 31 start's time is :1304690372656
Thread 30 start's time is :1304690372656
Thread 29 start's time is :1304690372656
Thread 25 start's time is :1304690372640
Thread 23 start's time is :1304690372640
Thread 24 start's time is :1304690372640
Thread 20 start's time is :1304690372640
Thread 21 start's time is :1304690372640
Thread 22 start's time is :1304690372640
Thread 17 start's time is :1304690372640
Thread 98 start's time is :1304690372671
。。。。省去了end time的打印
 

 

你可能感兴趣的:(Java,Concurrent,工作,多线程,thread,编程)