`
huhu_long
  • 浏览: 71789 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

多线程入门

阅读更多
一直只是看看多线程有关的东西, 项目中用的时候也只是网上查查, 能work就好, 而没有真正去研究过。 所以这次打算好好看一下。。。
------------------------ 我是分割线 ------------------------------
一个简单的多线程程序:
public class MyThread {

	private List<Thread> threads = new ArrayList<Thread>();

	public Thread makeThread() {
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				int i = 0;
				while (true) {
					System.out.println(Thread.currentThread().getName() + ": " + i++);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						return;
					}
				}
			}
		};

		return new Thread(runnable);
	}

	public synchronized void start() {
		for (int i = 0; i < 10; i++) {
			MyThread th = new MyThread();
			threads.add(th.makeThread());
		}
		for (Thread t : threads)
			t.start();
	}

	public synchronized void stop() {
		for (Thread t : threads)
			t.interrupt();
		threads.clear();
	}

	public static void main(String[] args) throws InterruptedException {
		System.out.println("All threads will start and end in 1 second...");
		System.out.println(new Date());
		MyThread mt = new MyThread();
		mt.start();
		Thread.sleep(1000 * 1);
		mt.stop();
		System.out.println(new Date());
	}
}


其某一次运行结果是:
引用
All threads will start and end in 1 second...
Tue Jul 19 14:11:47 CST 2011
Thread-0: 0
Thread-1: 0
Thread-2: 0
Thread-3: 0
Thread-4: 0
Thread-6: 0
Thread-5: 0
Thread-7: 0
Thread-8: 0
Thread-9: 0
Thread-2: 1
Thread-1: 1
Thread-6: 1
Thread-0: 1
Thread-4: 1
Thread-3: 1
Thread-7: 1
Thread-5: 1
Thread-9: 1
Thread-8: 1
Thread-2: 2
Thread-5: 2
Thread-7: 2
Thread-1: 2
Thread-6: 2
Thread-4: 2
Thread-0: 2
Thread-3: 2
Thread-9: 2
Thread-8: 2
Thread-2: 3
Thread-4: 3
Thread-6: 3
Thread-0: 3
Thread-5: 3
Thread-7: 3
Thread-1: 3
Thread-3: 3
Thread-8: 3
Thread-9: 3
Thread-1: 4
Thread-3: 4
Thread-0: 4
Thread-6: 4
Thread-7: 4
Thread-5: 4
Thread-2: 4
Thread-4: 4
Thread-9: 4
Thread-8: 4
Thread-3: 5
Thread-6: 5
Thread-2: 5
Thread-5: 5
Thread-7: 5
Thread-1: 5
Thread-0: 5
Thread-4: 5
Thread-8: 5
Thread-9: 5
Thread-2: 6
Thread-0: 6
Thread-4: 6
Thread-6: 6
Thread-1: 6
Thread-7: 6
Thread-5: 6
Thread-3: 6
Thread-8: 6
Thread-9: 6
Thread-2: 7
Thread-4: 7
Thread-0: 7
Thread-3: 7
Thread-7: 7
Thread-1: 7
Thread-5: 7
Thread-6: 7
Thread-8: 7
Thread-9: 7
Thread-2: 8
Thread-5: 8
Thread-1: 8
Thread-7: 8
Thread-3: 8
Thread-6: 8
Thread-0: 8
Thread-4: 8
Thread-9: 8
Thread-8: 8
Thread-5: 9
Thread-7: 9
Thread-2: 9
Thread-0: 9
Thread-1: 9
Thread-4: 9
Thread-6: 9
Thread-3: 9
Thread-8: 9
Thread-9: 9
Tue Jul 19 14:11:48 CST 2011


可以从运行结果中看出, 除了我们第一次启动的时候由于for的原因, 线程的启动是有序的。 但一旦线程启动之后, 其优先级就不是我们可控的了。

------------------------- 我是分隔线 -----------------------------
虽然我们可以同过setPriority方法来设置线程的优先级, 但是当线程数量不够多, 也就是说系统资源充足的时候很难看出优先级的存在。 通常的JVM实现都会在线程数目超过能使用的CPU数目时, 都会优先选择优先级比较高的线程运行。
一下程序是在上面实例的基础上加了优先级, 感官上没什么区别:

private int[] pri = new int[] { 1, 5, 10 };
private final Random r = new Random();

while (true) {
	System.out.println(Thread.currentThread().getName() + "(Priority = " + Thread.currentThread().getPriority() + "): "
			+ i++);
	try {
		Thread.sleep(100);
	} catch (InterruptedException e) {
		return;
	}
}

...

// Set priority
Thread t = new Thread(runnable);
t.setPriority(pri[r.nextInt(3)]);

...



运行结果:
引用
All threads will start and end in 1 second...
Tue Jul 19 14:47:11 CST 2011
Thread-0(Priority = 10): 0
Thread-2(Priority = 5): 0
Thread-1(Priority = 5): 0
Thread-3(Priority = 5): 0
Thread-4(Priority = 1): 0
Thread-7(Priority = 10): 0
Thread-5(Priority = 1): 0
Thread-9(Priority = 10): 0
Thread-6(Priority = 1): 0
Thread-8(Priority = 1): 0
Thread-2(Priority = 5): 1
Thread-0(Priority = 10): 1
Thread-7(Priority = 10): 1
Thread-1(Priority = 5): 1
Thread-3(Priority = 5): 1
Thread-4(Priority = 1): 1
Thread-5(Priority = 1): 1
Thread-9(Priority = 10): 1
Thread-8(Priority = 1): 1
Thread-6(Priority = 1): 1
Thread-7(Priority = 10): 2
Thread-2(Priority = 5): 2
Thread-0(Priority = 10): 2
Thread-3(Priority = 5): 2
Thread-1(Priority = 5): 2
Thread-4(Priority = 1): 2
Thread-6(Priority = 1): 2
Thread-8(Priority = 1): 2
Thread-5(Priority = 1): 2
Thread-9(Priority = 10): 2
Thread-0(Priority = 10): 3
Thread-3(Priority = 5): 3
Thread-7(Priority = 10): 3
Thread-2(Priority = 5): 3
Thread-1(Priority = 5): 3
Thread-4(Priority = 1): 3
Thread-9(Priority = 10): 3
Thread-5(Priority = 1): 3
Thread-6(Priority = 1): 3
Thread-8(Priority = 1): 3
Thread-7(Priority = 10): 4
Thread-1(Priority = 5): 4
Thread-3(Priority = 5): 4
Thread-0(Priority = 10): 4
Thread-2(Priority = 5): 4
Thread-4(Priority = 1): 4
Thread-9(Priority = 10): 4
Thread-5(Priority = 1): 4
Thread-8(Priority = 1): 4
Thread-6(Priority = 1): 4
Thread-1(Priority = 5): 5
Thread-2(Priority = 5): 5
Thread-3(Priority = 5): 5
Thread-0(Priority = 10): 5
Thread-7(Priority = 10): 5
Thread-9(Priority = 10): 5
Thread-8(Priority = 1): 5
Thread-6(Priority = 1): 5
Thread-4(Priority = 1): 5
Thread-5(Priority = 1): 5
Thread-1(Priority = 5): 6
Thread-3(Priority = 5): 6
Thread-2(Priority = 5): 6
Thread-0(Priority = 10): 6
Thread-7(Priority = 10): 6
Thread-9(Priority = 10): 6
Thread-8(Priority = 1): 6
Thread-4(Priority = 1): 6
Thread-5(Priority = 1): 6
Thread-6(Priority = 1): 6
Thread-1(Priority = 5): 7
Thread-0(Priority = 10): 7
Thread-2(Priority = 5): 7
Thread-3(Priority = 5): 7
Thread-7(Priority = 10): 7
Thread-9(Priority = 10): 7
Thread-4(Priority = 1): 7
Thread-5(Priority = 1): 7
Thread-8(Priority = 1): 7
Thread-6(Priority = 1): 7
Thread-0(Priority = 10): 8
Thread-3(Priority = 5): 8
Thread-2(Priority = 5): 8
Thread-1(Priority = 5): 8
Thread-9(Priority = 10): 8
Thread-7(Priority = 10): 8
Thread-4(Priority = 1): 8
Thread-6(Priority = 1): 8
Thread-8(Priority = 1): 8
Thread-5(Priority = 1): 8
Thread-0(Priority = 10): 9
Thread-1(Priority = 5): 9
Thread-3(Priority = 5): 9
Thread-2(Priority = 5): 9
Thread-7(Priority = 10): 9
Thread-9(Priority = 10): 9
Thread-8(Priority = 1): 9
Thread-4(Priority = 1): 9
Thread-6(Priority = 1): 9
Thread-5(Priority = 1): 9
Tue Jul 19 14:47:12 CST 2011
分享到:
评论

相关推荐

    Java多线程入门介绍.pdf

    ### Java多线程入门知识点详解 #### 一、引言 多线程是现代软件开发中的重要技术之一,尤其在Java编程语言中占有极其重要的地位。对于任何一位Java程序员而言,掌握多线程的基本概念和技术是非常必要的。本文将...

    关于多线程入门的最基础的知识(里面包括例程讲解)

    本资料包是针对多线程入门的学习资源,涵盖了多线程的基础概念、创建与管理以及同步控制等多个方面。 首先,让我们了解什么是多线程。在单线程系统中,程序的执行是顺序的,而多线程则允许在一个进程中同时运行多个...

    java多线程入门

    总之,Java多线程入门需要理解线程的基本概念、创建与管理,以及线程间通信和同步的各种机制。通过阅读《Java线程入门.pdf》这样的学习资料,结合实际编程实践,可以逐步掌握这门技术。在学习过程中,参考《E书说明....

    C++多线程入门.pdf

    1. C++多线程编程基础:文档标题“C++多线程入门.pdf”表明文档是关于C++多线程编程的入门级教程。文档描述没有提供更多信息,但可以假设文档会介绍C++多线程编程的基本概念、语法和应用。 2. C++标准库中多线程的...

    多线程入门,分布式锁,等相关资料

    本资源包提供了一套全面的多线程入门学习资料,帮助开发者深入理解这两个主题。 多线程是现代计算机编程中的基础概念,尤其是在Java这样的多线程支持语言中。在单核CPU时代,多线程主要用来实现程序的并发执行,...

    C++多线程入门[整理].pdf

    C++多线程入门[整理] 在现代计算机系统中,多线程编程是提高系统性能和响应速度的重要手段。C++作为一门面向对象的编程语言,提供了对多线程编程的支持。本文将从基本概念开始,介绍多线程编程的基本概念、线程的...

    DSPBIOS多线程入门级要点

    DSPBIOS多线程入门级要点 DSPBIOS多线程技术是DSP系统中的一种高级编程技术,能够满足多个非相关的程序或函数同时运行的需求。这些程序运行和执行时间都有严格的要求,这些程序被称为线程(thread)。在DSP中任何...

    C++多线程入门

    C++多线程入门

    delphi多线程入门实例

    本文将基于"delphi多线程入门实例"这个主题,深入讲解Delphi中如何实现简单的多线程。 首先,我们需要了解Delphi中的TThread类,它是VCL框架提供的一种用于创建线程的基础类。通过继承TThread类并重写其Execute方法...

    多线程分享.pptx 多线程入门简介

    多线程分享.pptx 多线程入门简介

    多线程入门详解多线程入门详解

    【多线程概念】 多线程是现代操作系统中一种重要的并发执行机制,它允许多个执行流在同一程序中并行运行。在Windows操作系统中,进程是资源分配的基本单位,而线程则是执行的基本单位。一个进程可以包含多个线程,...

    JAVA多线程入门基础知识

    必知必会的多线程入门基础知识

    iPhone开发之多线程入门示例程序

    在“iPhone开发之多线程入门示例程序”中,我们主要会接触到苹果的Foundation框架中的多线程解决方案,包括NSThread、NSOperation和GCD(Grand Central Dispatch)。 首先,NSThread是Objective-C中的一个类,它...

    linux多线程入门资料精选.zip

    本资源包"linux多线程入门资料精选.zip"包含两份重要的学习资料,旨在帮助初学者掌握这一技术。下面将对这两个主题进行详细阐述。 首先,"Linux网络编程.pdf"着重于讲解Linux环境下的网络通信基础。网络编程是所有...

    C多线程入门.pdf

    C多线程入门.pdf

    C多线程入门[整理].pdf

    C多线程入门[整理].pdf

    c# .net多线程入门录屏视频教程

    c# .net多线程入门录屏视频教程 ,适合刚入门的初学者

Global site tag (gtag.js) - Google Analytics