如何尽可能地使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的打印
 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
```java import java.util.LinkedList; import java.util.Queue; public class BoundedBlockingQueue<T> { private Queue<T> queue; private int capacity; public BoundedBlockingQueue(int capacity) { this.capacity = capacity; this.queue = new LinkedList<>(); } public synchronized void put(T item) throws InterruptedException { while (queue.size() == capacity) { wait(); } if (queue.size() == 0) { notifyAll(); } queue.add(item); } public synchronized T take() throws InterruptedException { while (queue.size() == 0) { wait(); } if (queue.size() == capacity) { notifyAll(); } return queue.remove(); } } ``` 解释: 这个工具类使用了 synchronized 和 wait/notifyAll 机制来达到多线程并发写入和阻塞取数的效果。 put 方法使用 synchronized 保证了多线程并发写入时的线程安全性。当队列已满时,使用 wait 方法阻塞当前线程,等待其他线程take元素后唤醒。当队列未满时,使用 notifyAll 方法唤醒其他等待的线程,使它们有机会去写入元素。 take 方法同样使用 synchronized 保证了多线程并发取数时的线程安全性。当队列为空时,使用 wait 方法阻塞当前线程,等待其他线程put元素后唤醒。当队列非空时,使用 notifyAll 方法唤醒其他等待的线程,使它们有机会去取出元素。 注意,这里使用了 while 循环来判断队列是否满或空,而不是 if,是因为在多线程情况下,可能存在多个线程同时进行判断,只有使用 while 循环才能保证每个线程都正确地进行判断。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值