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调用的。

通过源码总结要点:

  1. Thread被构建后的NEW状态下,threadStatus这个内部属性值为0;

  2. 不能两次启动Thread,不然就会出现IllegalThreadStateException异常;

  3. group.threadStartFailed(this);可以看出线程启动后将会被加入到一个ThreadGroup中;

  4. 一个线程生命周期到了TERMINATED状态,是不允许调用start方法的。

总结:jdk对start和run的设计,其实就是一个典型的模板模式,也就是父类编写算法结构代码,具体的逻辑实现由子类去完成。

  获取实时信息,关注公众号:『编程之艺术』,二维码:

最新文章

  1. .NET开源高性能Socket通信中间件Helios介绍及演示
  2. 简单的freemarker解析测试
  3. scala环境搭建
  4. codeforces 2B The least round way 【DP】
  5. Canvas入门(2):图形渐变和图像形变换
  6. ios AFNetworking 有用篇
  7. 【HDU1102】Constructing Roads(MST基础题)
  8. Core Data 应用程序实践指南(Core Data 应用程序实践指南)
  9. nodeJS之eventproxy源码解读
  10. Activiti第二篇【管理流程定义、执行任务和流程实例、流程变量】
  11. 创建Android项目及常见错误解决
  12. c语言求最大公约数
  13. Vue.js最简单的代码
  14. Swift - 多个mask的动画效果
  15. python -- 装饰器入门
  16. 9.Node.js 包管理器npm
  17. 修改eclipse中文件打开默认方式
  18. Jenkins 自动化部署asp.net
  19. 深挖 NGUI 基础 之UICamera (二)
  20. Python+Selenium练习篇之2-利用ID定位元素

热门文章

  1. iconfontのsymbol的使用
  2. VUE过滤器的使用 vue 时间格式化
  3. 成为高级 React 开发你需要知道的知识点
  4. dedecms新建内容模型“把数据保存到数据库附加表时出错‘xxx’出错”错误的原因分析和解决方案(转)
  5. 基于dbunit进行mybatis DAO层Excel单元测试
  6. vue+ elementUI 打包发布到服务器后,element-icons.woff文件404
  7. 【iOS】Interface Builder 预览
  8. android蓝牙通讯开发(详细)
  9. 【Spring源码解析】—— 委派模式的理解和使用
  10. JavaSE(一)Java程序的三个基本规则-组织形式,编译运行,命名规则