线程的start方法解析
Thread是学习我们学习多线程接触到的第一个有关多线程的类,相信每一个学习过或者了解过Java多线程的小伙伴都知道Thread类。这次分享主要对Thread的start方法进行讲解。
相信大家都知道,start方法是启动一个线程,并且该线程进入了可执行状态。在实际的编码中,我们是重写run()方法,调用start()方法启动线程,那么run()和start()方法有什么联系呢?下面我们就详细说说。
一、源码分析
首先我们查看start的源码,如下:
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException(); /* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this); boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
start()方法是非常简单的,如果从上面单独看上面源码是看不出任何端倪的,不过我们可以看出来Thread被创建之后,threadStatus这个内部属性的值是0 。在上面源码中,最核心的部分就是start0();这个本地方法,也就是我们经常能在其他博文中看到的JNI方法,所谓JNI方法其实没有那么高大上,其实JNI就是Java平台用于和本地C代码进行互操作的API,JNI不支持Java类和 C++类之间的任何映射机制,这里只是简单的提一句。为什么说start0();是最核心的部分呢?看下图:
那么我们重写的run方法是何时被调用的呢?可以看start方法的注释,如下图:

Causes this thread to begin execution; the Java Virtual Machine
calls the <code>run</code> method of this thread.
这段注释是说:在开始执行这个线程时,JVM就会调用run方法,也就是说run方法就是被JNI方法start0调用的。
通过源码总结要点:
Thread被构建后的NEW状态下,threadStatus这个内部属性值为0;
不能两次启动Thread,不然就会出现IllegalThreadStateException异常;
group.threadStartFailed(this);可以看出线程启动后将会被加入到一个ThreadGroup中;
一个线程生命周期到了TERMINATED状态,是不允许调用start方法的。
总结:jdk对start和run的设计,其实就是一个典型的模板模式,也就是父类编写算法结构代码,具体的逻辑实现由子类去完成。
获取实时信息,关注公众号:『编程之艺术』,二维码:

线程的start方法解析的更多相关文章
- 线程的Interrupt方法与InterruptedException解析
线程阻塞状态与等待状态(当一个线程处于被阻塞或等待状态时,它暂时不活动,不允许任何代码且消耗最少的资源) 当一个线程试图获得一个内部的对象锁(而不是java.util.concurrent库中的锁), ...
- 线程sleep,wait,notify,join,yield方法解析
线程的五种状态 线程从创建到销毁一般分为五种状态,如下图: 1) 新建 当用new关键字创建一个线程时,就是新建状态. 2) 就绪 调用了 start 方法之后,线程就进入了就绪阶段.此时,线程不会立 ...
- Java线程Thread的状态解析以及状态转换分析 多线程中篇(七)
线程与操作系统中线程(进程)的概念同根同源,尽管千差万别. 操作系统中有状态以及状态的切换,Java线程中照样也有. State 在Thread类中有内部类 枚举State,用于抽象描述Java线程的 ...
- 并发之线程封闭与ThreadLocal解析
并发之线程封闭与ThreadLocal解析 什么是线程封闭 实现一个好的并发并非易事,最好的并发代码就是尽量避免并发.而避免并发的最好办法就是线程封闭,那什么是线程封闭呢? 线程封闭(thread c ...
- 几种线程安全的Map解析
转载自 面试必问-几种线程安全的Map解析 HashMap线程安全的吗? Java中平时用的最多的Map集合就是HashMap了,它是线程不安全的. 看下面两个场景: 1.当用在方法内的局部变量时,局 ...
- 一个简单的wed服务器SHTTPD(5)————服务器SHTTPD请求方法解析
//start from the very beginning,and to create greatness //@author: Chuangwei Lin //@E-mail:979951191 ...
- Python的方法解析顺序(MRO)[转]
本文转载自: http://hanjianwei.com/2013/07/25/python-mro/ 对于支持继承的编程语言来说,其方法(属性)可能定义在当前类,也可能来自于基类,所以在方法调用时就 ...
- sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO
sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO 今天在弄一个 sqlalchemy 的数据库基类的时候,遇到了跟多继承相关的一个小问题,因此顺便看了一 ...
- 线程的Abort方法有感
今天看CSDN上一个很老的帖子,有个人说Thread.Abort()方法调用之后一定会抛出异常,我对这个有点疑问. 于是自己做了一个测试demo,来研究Abort抛出异常的时机.废话少说,直接上代码: ...
随机推荐
- 关于使用 AJax 生成Form表单,且表单提交需要验证,验证实效的解决方法
@Ajax.ActionLink("添加", "AddUser",new AjaxOptions() {InsertionMode = InsertionMod ...
- Jmeter--录制脚本-用户参数化-添加断言
使用jmeter实现的场景 1.使用badboy录制脚本 2.使用jmeter自带元件进行用户参数化 3.给请求添加断言(给请求添加检查点) 使用badboy录制脚本导入jmeter 1.输入http ...
- python面向过程编程 - ATM
前面程序整合加自定义日志 1.文件摆放 ├── xxxx │ ├── src.py │ └── fil_mode.py │ └── data_time.py │ └── loading.py │ └─ ...
- Android总结之打开手机相册获取图片
上一篇,总结了如何打开照相机获取图片,详情请看>>>> 这篇将总结如何打开手机存储(相册)来获取手机上的图片. 打开相册 在需要这个功能的类中,我们可以自定义一个方法openA ...
- linux中type 、file、stat三者的区别
1.type 用来查看命令类型,以区别是内部命令还是外部命令 示例:[root@localhost ~]# type cd cd 是 shell 内嵌 [root@localhost ~]# t ...
- mysql_fetch_assoc与mysql_fetch_array的区别
mysql_fetch_assoc与mysql_fetch_array的区别? 1. mysql_fetch_assoc : mysql_fetch_assoc() 函数从结果集中取得一行作为关联数组 ...
- 代码中批量执行Oracle SQL语句
今天在写一个工具(winform),作用是批量的INSERT OR UPDATE ORACLE数据库中的一个表. 执行的时候老是报错“[911] ORA-00911: invalid charact ...
- Sublime Text 格式化代码
1.添加快捷键 其实在sublime中已经自建了格式化按钮: Edit -> Line -> Reindent 只是sublime并没有给他赋予快捷键,所以只需加上快捷键即可 Prefer ...
- python课堂整理4---列表的魔法
一.list 类, 列表 li = [1, 12, 9, "age", ["大白", "小黑"], "alex"] ...
- .Net微信网页开发之使用微信JS-SDK调用微信扫一扫功能
前言: 之前有个项目需要调用微信扫描二维码的功能,通过调用微信扫码二维码功能,然后去获取到系统中生成的二维码信息.正好微信JS-SDK提供了调用微信扫一扫的功能接口,下面让我们来看看是如何实现的吧. ...