`

如何尽可能地使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
分享到:
评论

相关推荐

    vc++中的线程锁(线程锁保持线程同步)

    在VC++编程环境中,线程同步是一个至关重要的概念,特别是在多线程程序设计中,以确保并发执行的线程能够安全地访问共享资源,避免数据竞争和其他潜在的问题。本篇文章将详细探讨线程锁在VC++中的应用,以及如何通过...

    C# 高效线程安全,解决多线程写txt日志类.zip

    3. **日志缓冲区**:使用队列作为日志缓冲区,线程安全地将日志项添加到队列,然后由单独的后台线程负责从队列中取出并写入文件,避免了多个线程同时写入的冲突。 4. **日志文件切分**:当单个日志文件大小达到预设...

    MFC GDI多线程绘图

    - 尽量减少跨线程访问GDI对象,尽可能在创建线程时一次性分配所需的GDI资源,减少共享。 - 使用`DeleteObject()`释放不再需要的GDI对象,避免资源泄露。 - 考虑使用GDI+或Direct2D等现代图形库,它们提供了更高级...

    开启一个线程加载一张图片

    标签"一个线程"强调了我们关注的是单个后台线程,这意味着我们不会使用过于复杂的并发策略,而是尽可能保持代码简洁,减少线程间的交互和同步开销。 综上所述,开启一个线程加载一张图片涉及了多线程编程、并发控制...

    C#线程参考手册 线程学习

    每个线程都有一个优先级,但操作系统并不保证优先级高的线程一定先执行,而是尽可能给予较高优先级的线程更多执行机会。 六、后台线程与前台线程 1. 前台线程:与进程生命周期绑定,只有所有前台线程都结束,进程...

    servlet与Struts action线程安全问题分析

    1. **避免使用实例变量**:尽可能使用局部变量,局部变量只存在于方法的执行上下文中,不会被多个线程共享,因此不存在线程安全问题。 2. **使用同步控制**:通过`synchronized`关键字对关键代码块或方法进行同步,...

    多线程相关资料

    - 减少共享资源的使用,尽可能让数据私有化。 - 使用适当的同步策略,避免不必要的阻塞。 通过学习和实践这些知识点,开发者能够有效地编写出高效、稳定且具有并发能力的多线程程序,提升软件的性能和响应速度。...

    监控线程原型

    6. **性能优化**:监控线程需要尽可能地减少对系统资源的占用,以不影响被监控线程的正常运行。 在实际项目中,"monitor"这个文件可能包含了监控线程的源代码或者设计文档,用于指导开发者实现这一功能。分析和理解...

    mfc多线程购票

    设计时需注意避免无谓的线程创建和销毁,尽可能复用线程,或者使用线程池来提高效率。 通过以上步骤,我们可以构建一个安全、高效的MFC多线程购票系统。这个系统不仅需要处理并发访问,还需要保证用户体验,因此对...

    易语言源码易语言UDP工作线程源码.rar

    1. UDP协议:UDP是一种无连接的、不可靠的传输层协议,它不提供数据包的顺序保证和错误恢复,而是以尽可能快的速度发送数据。在网络游戏、在线直播等领域中,由于对实时性的要求较高,常选用UDP协议。 2. 工作线程...

    多线程编程指南

    - **4.4.1 完全避免同步**:尽可能使用不可变数据结构或其他无锁技术。 - **4.4.2 了解同步的限制**:识别和理解同步机制的局限性。 - **4.4.3 注意对代码正确性的威胁**:识别可能导致错误的潜在因素。 - **4.4.4 ...

    .Net线程详细介绍

    .Net线程详解 在.NET框架中,线程是并发执行任务的基本单元。本文将深入探讨.NET线程的创建、类型、同步机制...务必注意,只有在访问共享资源时才需要锁定,并尽可能使用轻量级同步机制,如`lock`,以减少系统开销。

    多线程处理数据(工具)样例

    2. **负载均衡**:在分配任务给线程时,要尽可能使每个线程的工作负载均衡,避免某些线程过早结束而其他线程还在忙碌,这样可以最大化利用所有资源。 3. **线程池**:创建和销毁线程都有一定的开销,使用线程池可以...

    第一节(多线程通讯代码演示及解决线程安全问题)

    在实际编程中,我们还应遵循一些最佳实践,例如,尽量减少共享状态,使用不可变对象,使用局部变量代替共享变量,以及尽可能使用线程局部存储(Thread Local Storage)等。 通过学习和理解上述知识点,并结合具体的...

    CVI 03.多线程数据保护(线程锁)

    - **减少锁的粒度**:尽可能缩小受保护代码的范围,以降低锁的持有时间,提高并发性能。 - **避免活锁**:当线程不断重试而无法取得锁,可能导致系统无休止地循环。通过设置超时或随机退让策略可以防止活锁。 - **...

    java线程安全总结.doc

    Java线程安全是多线程编程中的一个关键概念,它涉及到在并发环境下如何正确地管理共享资源,确保程序的正确性和一致性。以下是对Java线程安全的深入总结: ### 一、线程安全的定义 线程安全是指当多个线程访问同一...

    多线程 技术文档

    - **减少竞争条件**:尽可能减少对共享资源的竞争,使用局部变量或线程局部存储来替代全局变量。 - **线程池**:利用线程池管理线程资源,减少线程的创建和销毁带来的开销。 - **异步编程模型**:采用异步编程模型如...

    多线程同步利用ccriticalsection

    - 临界区应尽可能短,以减少其他线程等待的时间,提高系统效率。 - 不要在临界区内调用可能引起长时间阻塞的操作,如I/O操作或网络通信,否则可能导致其他线程饿死。 6. **学习资源**: - 对于初学者,可以从...

    Java多线程文档

    在IT领域,尤其是在Java编程中,多线程是不可或缺的一部分,它使得程序可以在同一时间执行多个不同的任务,极大地提高了效率。这篇"Java多线程文档"涵盖了关于Java多线程编程的重要知识点,以下是对这些内容的详细...

    .net 线程参考手册

    3. **合理选择同步机制**:根据具体场景选择合适的同步策略,既保证线程安全,又尽可能减少锁的使用,提高并发性能。 通过深入学习《.NET线程参考手册》,开发者能够更加熟练地掌握.NET框架下的多线程编程技术,为...

Global site tag (gtag.js) - Google Analytics