Thread源码分析
本文为转载,请珍惜别人的劳动成果,注明转载地址:http://www.cnblogs.com/gw811/archive/2012/10/15/2724602.html
1、Runnable接口源码:
1 public interface Runnable {
2 public abstract void run();
3 }
2、Thread类与Runnable接口的继承关系
1 public class Thread implements Runnable{
2
3 }
Runnable接口仅有一个run()方法,Thread类实现了Runnable接口,所以,Thread类也实现了Runnable接口。
3、构造函数
1 public Thread() {
2 init(null, null, "Thread-" + nextThreadNum(), 0);
3 }
1 public Thread(Runnable target) {
2 init(null, target, "Thread-" + nextThreadNum(), 0);
3 }
1 public Thread(ThreadGroup group, Runnable target) {
2 init(group, target, "Thread-" + nextThreadNum(), 0);
3 }
1 public Thread(String name) {
2 init(null, null, name, 0);
3 }
还有其它的构造方法,此处省略。。。
这里的第三个参数是设置线程的名称,从下面的代码中可以看出,生成名称的规则是:”Thread-”加上创建的线程的个数(第几个)。
继续查看init方法:

1 /**
2 * Initializes a Thread.
3 *
4 * @param g the Thread group
5 * @param target the object whose run() method gets called
6 * @param name the name of the new Thread
7 * @param stackSize the desired stack size for the new thread, or
8 * zero to indicate that this parameter is to be ignored.
9 */
//ThreadGroup:线程组表示一个线程的集合。此外,线程组也可以包含其他线程组。线程组构成一棵树,在树中,除了初始线程组外,每个线程组都有一个父线程组。
10 private void init(ThreadGroup g, Runnable target, String name,
11 long stackSize) {
12 Thread parent = currentThread();
13 SecurityManager security = System.getSecurityManager();
14 if (g == null) {
15 /* Determine if it's an applet or not */
16
17 /* If there is a security manager, ask the security manager
18 what to do. */
19 if (security != null) {
20 g = security.getThreadGroup();
21 }
22
23 /* If the security doesn't have a strong opinion of the matter
24 use the parent thread group. */
25 if (g == null) {
26 g = parent.getThreadGroup();
27 }
28 }
29
30 /* checkAccess regardless of whether or not threadgroup is
31 explicitly passed in. */
32 g.checkAccess();
33
34 /*
35 * Do we have the required permissions?
36 */
37 if (security != null) {
38 if (isCCLOverridden(getClass())) {
39 security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
40 }
41 }
42
43
44 g.addUnstarted();
45
46 this.group = g;
//每个线程都有一个优先级,高优先级线程的执行优先于低优先级线程。每个线程都可以或不可以标记为一个守护程序。当某个线程中运行的代码创建一个新Thread 对象时,该新线程的初始优先级被设定为创建线程的优先级,并且当且仅当创建线程是守护线程时,新线程才是守护程序。
47 this.daemon = parent.isDaemon();
48 this.priority = parent.getPriority();
49 this.name = name.toCharArray();
50 if (security == null || isCCLOverridden(parent.getClass()))
51 this.contextClassLoader = parent.getContextClassLoader();
52 else
53 this.contextClassLoader = parent.contextClassLoader;
54 this.inheritedAccessControlContext = AccessController.getContext();
55 this.target = target;
56 setPriority(priority);
57 if (parent.inheritableThreadLocals != null)
58 this.inheritableThreadLocals =
59 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
60 /* Stash the specified stack size in case the VM cares */
61 this.stackSize = stackSize;
62
63 /* Set thread ID */
64 tid = nextThreadID();
65 }

初始化时设置了是否为守护线程,优先级,初始化名称。
4、Thread的start方法的实现:

1 public synchronized void start() {
2 /**
3 * This method is not invoked for the main method thread or "system"
4 * group threads created/set up by the VM. Any new functionality added
5 * to this method in the future may have to also be added to the VM.
6 *
7 * A zero status value corresponds to state "NEW".
8 */
9 if (threadStatus != 0)
10 throw new IllegalThreadStateException();
11 group.add(this);
12 start0();
13 if (stopBeforeStart) {
14 stop0(throwableFromStop);
15 }
16 }

这里主要的是start0方法;查看其实现:
1 private native void start0();
这里使用了本地调用,通过C代码初始化线程需要的系统资源。可见,线程底层的实现是通过C代码去完成的。
4、Thread的run方法的实现
1 public void run() {
2 if (target != null) {
3 target.run();
4 }
5 }
这里的target实际上要保存的是一个Runnable接口的实现的引用:
1 private Runnable target;
所以使用继承Thread创建线程类时,需要重写run方法,因为默认的run方法什么也不干。
而当我们使用Runnable接口实现线程类时,为了启动线程,需要先把该线程类实例初始化一个Thread,实际上就执行了如下构造函数:
1 public Thread(Runnable target) {
2 init(null, target, "Thread-" + nextThreadNum(), 0);
3 }
即是把线程类的引用保存到target中。这样,当调用Thread的run方法时,target就不为空了,而是继续调用了target的run方法,所以我们需要实现Runnable的run方法。这样通过Thread的run方法就调用到了Runnable实现类中的run方法。
这也是Runnable接口实现的线程类需要这样启动的原因。
Thread源码分析的更多相关文章
- Handler、Looper、MessageQueue、Thread源码分析
关于这几个之间的关系以及源码分析的文章应该挺多的了,不过既然学习了,还是觉得整理下,印象更深刻点,嗯,如果有错误的地方欢迎反馈. 转载请注明出处:http://www.cnblogs.com/John ...
- Thread源码分析-java8
1.Thread特性分析 守护线程Daemon 定性:支持性线程,主要用于程序中后台调度以及支持性工作. 当JVM中不存在Daemon线程时,JVM将会退出. 将一个线程设定为Daemon的方法: 调 ...
- 并发编程学习笔记(七、Thread源码分析)
目录: 常见属性 构造函数 start() run() 常见属性: /** * 线程名称 */ private volatile String name; /** * 线程优先级 */ private ...
- java Thread源码分析(二)
一.sleep的使用 public class ThreadTest { public static void main(String[] args) throws InterruptedExcept ...
- java Thread源码分析
一.使用 java 多线程 java多线程其中两种使用方式: 1.继承 Thread 类 2.实现 Runnable 接口 public class ThreadTest { public stati ...
- JNI-从jvm源码分析Thread.interrupt的系统级别线程打断原理
前言 在java编程中,我们经常会调用Thread.sleep()方法使得线程停止运行一段时间,而Thread类中也提供了interrupt方法供我们去主动打断一个线程.那么线程挂起和打断的本质究竟是 ...
- 【Android笔记】Thread类中关于join()方法的源码分析
1.join()方法的作用: 例如有一个线程对象为Thread1,在main()方法中调用Thread1.join()方法可使得当前线程(即主线程)阻塞,而执行Thread1线程. 2.源码分析(以上 ...
- zookeeper源码分析之五服务端(集群leader)处理请求流程
leader的实现类为LeaderZooKeeperServer,它间接继承自标准ZookeeperServer.它规定了请求到达leader时需要经历的路径: PrepRequestProcesso ...
- zookeeper源码分析之四服务端(单机)处理请求流程
上文: zookeeper源码分析之一服务端启动过程 中,我们介绍了zookeeper服务器的启动过程,其中单机是ZookeeperServer启动,集群使用QuorumPeer启动,那么这次我们分析 ...
随机推荐
- Linux Process/Thread Creation、Linux Process Principle、sys_fork、sys_execve、glibc fork/execve api sourcecode
相关学习资料 linux内核设计与实现+原书第3版.pdf(.3章) 深入linux内核架构(中文版).pdf 深入理解linux内核中文第三版.pdf <独辟蹊径品内核Linux内核源代码导读 ...
- Process manufacturing和Discrete manufacturing的区别
Process manufacturing(Process industry) 加工制造,或者加工工业.其一个重要特征是,原材料被加工成成品后,我们再也无法将它恢复成原料,比如,苹果罐头,我们再没法把 ...
- C#图片读取和保存
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- php编程常用经验
1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量, 单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的"函数&quo ...
- 使用Tengine替代Nginx作为负载均衡服务器
Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检 ...
- c/c++细节知识整理
这篇文章总结了部分c/c++琐碎的细节知识. 目录如下: (一)bool类型 知识点出处较多,无法一一列举,向原作者致敬. (一)bool类型 在c99标准以前,c语言并没有定义bool类型.如果需要 ...
- C# 集合扩展快速排序算法
/// <summary> /// 对集合进行排序,如 /// List<Person> users=new List<Person>(){.......} /// ...
- C语言中strstr函数
头文件:#include <string.h> strstr()函数用来检索子串在字符串中首次出现的位置,其原型为: char *strstr( char *str, char * ...
- 设置dom元素可拖动,支持ie5+
摘要: 最近在项目中要做一个图片预览的功能,这时候会遇到用户上传很大的图片,已经超出视图界面.最终决定做一个在固定宽和高的位置,用户可以拖动图片查看.所以自己就写了一个支持ie5+,chrome,Fi ...
- 支持高并发的IIS Web服务器常用设置 II
适用的IIS版本:IIS 7.0, IIS 7.5, IIS 8.0 适用的Windows版本:Windows Server 2008, Windows Server 2008 R2, Windows ...