Java Thread源码分析
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接口实现的线程类需要这样启动的原因。
转载于:https://www.cnblogs.com/gw811/archive/2012/10/15/2724602.html
Java Thread源码分析相关推荐
- java web开源项目源码_超赞!推荐一个专注于Java后端源码分析的Github项目!
大家好,最近有小伙伴们建议我把源码分析文章及源码分析项目(带注释版)放到github上,这样小伙伴们就可以把带中文注释的源码项目下载到自己本地电脑,结合源码分析文章自己本地调试,总之对于学习开源项目源 ...
- Thread源码分析之join方法
2019独角兽企业重金招聘Python工程师标准>>> join方法示例1 源码 import java.util.concurrent.TimeUnit;public class ...
- Java IO源码分析(四)——PrintStream
简介 PrintStream继承于FilterOutputStream,而FilterOutputStream用于封装其他的输出流. PrintStream用于给其他的输出流封装了一层打印的功能,它内 ...
- Thread源码分析-java8
1.Thread特性分析 守护线程Daemon 定性:支持性线程,主要用于程序中后台调度以及支持性工作. 当JVM中不存在Daemon线程时,JVM将会退出. 将一个线程设定为Daemon的方法: 调 ...
- Java集合源码分析(二)ArrayList
ArrayList简介 ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境下,多线 ...
- Java Thread 源码解析
Thread 源码解析 线程的方法大部分都是使用Native使用,不允许应用层修改,是CPU调度的最基本单元.线程的资源开销相对于进程的开销是相对较少的,所以我们一般创建线程执行,而不是进程执行. T ...
- java abstractlist_源码分析-java-AbstractList-Itr和ListItr的实现
AbstractList API文档 AbstractList实现了List接口,又因为List继承自Collection,Collection继承自Iterable.因此List接口包含很多的方法. ...
- Java Stream源码分析及知识点总结
概述 什么是Stream Stream就是一种流式的处理数据风格,这一种风格将要处理的元素集合看作一种流,流在管道中传输,并且可以在管道的节点上进行处理,比如进行筛选.排序和聚合.通俗地说,就是将St ...
- 【java】java boolean 源码分析
1.概述 转载:jdk源码分析-------------boolean 这篇文章小编只是对boolean类型的几个方法进行一次知识梳理,大家有好的意见可以在留言区留下宝贵的意见,小编会做出相应的调整. ...
最新文章
- Cisco网院成立10周年-未来三年在蓉增50所
- SAP MM不常用功能之供应商Rebate
- ssh命令、ping命令、traceroute 命令所使用的协议
- [概统]本科二年级 概率论与数理统计 第七讲 期望、方差与相关性
- Zookeeper基于Java访问-权限
- 使用Apache Camel进行负载平衡
- 关于韩京清学者的《直线型倒立摆的自抗扰控制设计方案》仿真
- 《编码规范和测试方法——C/C++版》学习笔记 ·001
- 质性研究工具_MAXQDA在质性数据分析中的使用——免费的中文在线研讨会!
- Hudson dryRun jobs break after upgrade from 2.2.0 to 2.2.1
- linux下检查是否安装过某软件包
- NAACL 2019自然语言处理亮点
- C# 实现打开和关闭可执行文件
- JAVA匿名实现多线程
- php api 版本控制,PHP-API接口版本控制的问题
- Mysql中间件代理 Atlas
- 嵌入式C语言自我修养分享课件
- Fileupload
- AP与CP介绍【转】
- 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java鲲龙装饰公司在线管理系统的设计与开发前台模块iub6h