Java线程Run和Start的区别
先上结论: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方法。
Java线程Run和Start的区别的更多相关文章
- Java线程—-Runnable和Callable的区别和联系
Java 提供了三种创建线程的方法 1.继承Thread接口 public class Thread2Thread { public static void main(String[] args) { ...
- JAVA线程sleep和wait方法区别
一. sleep 是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复,调用sleep 不会释放对象锁.由于没有释放对象锁,所以不能 ...
- Java线程wait和sleep的区别
Java中调用wait方法或者sleep方法都可以让线程进入waitint或者time-waiting状态,但是它们还是 有所不同的: wait是Object中的方法,而sleep则是Thread中的 ...
- JAVA线程sleep和wait方法区别 代码
package test; import java.util.Date; import java.util.Random; public class test { public static void ...
- java线程中yield(),sleep(),wait()区别详解
1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...
- Java Thread 的 run() 与 start() 的区别
Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别 1. ...
- Java线程中yield与join方法的区别
长期以来,多线程问题颇为受到面试官的青睐.虽然我个人认为我们当中很少有人能真正获得机会开发复杂的多线程应用(在过去的七年中,我得到了一个机会),但是理解多线程对增加你的信心很有用.之前,我讨论了一个w ...
- JAVA线程和进程区别
1,JAVA线程和进程区别? (1)简单来讲一个运行的程序就是一个进程,一个进程中可以有多个线程(线程是程序执行的最小单元). (2)线程有四种状态:运行,就绪,挂起,结束 (3)使用多线程的好处 使 ...
- java 线程同步 原理 sleep和wait区别
java线程同步的原理java会为每个Object对象分配一个monitor, 当某个对象(实例)的同步方法(synchronized methods)被多个线程调用时,该对象的monitor将负责处 ...
随机推荐
- JNI由浅入深_7_c调用Java方法一
1.在Java中声明方法 <span style="font-size:14px;">/** * javah -encoding utf-8 -jni com.exam ...
- POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)
传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- CentOS7 搭建RabbitMQ集群 后台管理 历史消费记录查看
简介 通过 Erlang 的分布式特性(通过 magic cookie 认证节点)进行 RabbitMQ 集群,各 RabbitMQ 服务为对等节点,即每个节点都提供服务给客户端连接,进行消息发送与接 ...
- 基于.net core 微服务的另类实现
基于.net core 的微服务,网上很多介绍都是千篇一律基于类似webapi,通过http请求形式进行访问,但这并不符合大家使用习惯.如何像形如[ GetService<IOrderServi ...
- iOS开发--MQTT实时处理数据
一. MQTT 一个物联网项目中用到了MQTT协议, 可以用来做设备与软件之间的互通. MQTT: 即时通讯协议, 传输层协议 二. 常用: 1.MQTTKit(已经不维护了) 2.MQTTClien ...
- 关于iOS Block当中为什么要用weakSelf和strongSelf的思考
场景:当你在某个界面请求网络数据的时候,用户不愿意等待点击了返回按钮,此时在Block当中用如下的方式使用weakSelf的话,有可能会奔溃(因为在并发编程的情况下,虽然在if判断的时候weaksel ...
- ionic3 返回多个页面的写法
直接上代码 ionic3 返回2步 3步 或者多部 this.navCtrl.popTo(this.navCtrl.getByIndex(this.navCtrl.length()-3)); ...
- C++练习 | 模板与泛式编程练习
#include <iostream> #include <cmath> #include <cstring> #include <string> #i ...
- VS Code 的常用快捷键及插件(前端)
一.vs code 的常用快捷键 1.注释: a) 单行注释:[ctrl+k,ctrl+c] 或 ctrl+/ b) 取消单行注释:[ctrl+k,ctrl+u] (按下ctrl不放,再按k + u) ...
- MySQL----MySQL数据库入门----第二章 数据库和表的基本操作
2.1 数据库和数据库表的创建 ①数据库的创建(在数据库系统中划分一块存储数据的空间): create database 数据库名称 [charset 字符集]: ②数据库表的创建 use 数据库名 ...