先上结论:run只是Thread里面的一个普通方法,start是启动线程的方法。何以见得呢?可以执行下面的代码看看run和start的区别:

package com.basic.thread;

/**
* @author zhangxingrui
* @create 2019-02-16 20:12
**/
public class TestRunAndStart { private static void sayHello(){
System.out.println("hello, world");
} public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
sayHello();
System.out.println("Current thread: " + Thread.currentThread().getName());
}
}); thread.run();
thread.start();
} }

  执行结果:

  由此可以看到子线程是由start来启动的,里面调用了run,所以打印出来的是子线程的name。

  另外也可以从start方法的底层代码看到,首先进入start方法里面

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 */
}
}
}

  里面调用了start0,继续跟进

private native void start0();

  哦,这里就调了native方法了,再往下我们就看不到了。但是可以去openjdk的源码里面去看一下,我用的是jdk8。

  去openjdk看源码

  https://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/f0b93fbd8cf8/src/share/native/java/lang/Thread.c

  

  这里调用了JVM_StartThread,在jvm.h里面

  继续进入

  https://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/76a9c9cf14f1/src/share/vm/prims/jvm.cpp

  

  重点看最下面的语句native_thread = new JavaThread(&thread_entry, sz),好,new JavaThread的时候传入了thread_entry,我们再去

看一下啊thread_entry是什么:

  

  在thread_entry这个函数里面调用call_virtual方法,重点是它传入的参数run_method_name,从这里我们就知道了,噢,

原来start最终会在新线程里面调用run方法。

 

最新文章

  1. inverse
  2. IOS_ios逆向工程-静态分析
  3. DOM和DHTML等,复习总结
  4. 通过Ztree生成页面html元素Dom树,以及拖拽改变元素的位置
  5. 发现前端框架 bui-min.js
  6. js的严谨模式
  7. cocopods的安装和使用
  8. ubuntu14.04修复启动项
  9. 获取设备mac地址和md5加密
  10. Filter plugins ? mutate:
  11. 第一个程序 - Windows程序设计(SDK)001
  12. jQuery报错:
  13. hone hone clock人体时钟
  14. AnsiIO
  15. 201521123011《Java程序设计》第14周学习总结
  16. mysql5.6.24的安装与简单使用
  17. ARKit 增强现实平台 尝试(Xcode9 iOS11 A9处理器)
  18. voip技术研究
  19. qt pro 设置编译参数,支持 xp
  20. 痞子衡嵌入式:语音处理工具Jays-PySPEECH诞生记(1)- 环境搭建(Python2.7.14 + PyAudio0.2.11 + Matplotlib2.2.3 + SpeechRecognition3.8.1 + pyttsx3 2.7)

热门文章

  1. disconf实践(三)基于XML的分布式配置文件管理,自动reload
  2. Linux文本处理
  3. Easyui之accordion修改Title样式,字体等
  4. c++高精度计算(加法)
  5. 小白该如何学习Linux操作系统
  6. 多线程深入理解和守护线程、子线程、锁、queue、evenet等介绍
  7. 前端框架---jQuery---一分钟下载使用
  8. Noip 2016 Day 1 & Day 2
  9. OAuth(开放授权)
  10. 20155217 2016-2017-2 《Java程序设计》第2周学习总结