1、为什么启动线程不用run()方法而是使用start()方法

run()方法只是一个类中的普通方法,调用run方法跟调用普通方法一样

而start()是创建线程等一系列工作,然后自己调用run里面的任务内容。

验证代码:

/**
* @data 2019/11/8 - 下午10:29
* 描述:run()和start()
*/
public class StartAndRunMethod {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
};
runnable.run(); new Thread(runnable).start();
}
}

结果:

main
Thread-0


2、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();

    关于threadStatus源码:

  •     /*
    * Java thread status for tools, default indicates thread 'not yet started'
    */
    private volatile int threadStatus;

    通过代码可以看到就是threadStatus就是记录Thread的状态,初始线程默认为0.

  • 加入线程组
  •  /* 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);
  • 调用start0()
  • 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()方法使用c++编写的方法,这些代码在gdk代码中,所以这里不再这里探究了。


3、start()方法不能使用多次

通过刚刚源码分析,就知道start方法刚开始就检查线程状态,当线程创建后或结束了,该状态就不同于初始化状态就会抛出

IllegalThreadStateException异常。

测试代码:

/**
* @data 2019/11/8 - 下午11:57
* 描述:start不可以使用多次
*/
public class CantStartTwice {
public static void main(String[] args) {
Thread thread = new Thread();
thread.start();
thread.start();
}
}

start不可以使用多次



 4、注意点:

start方法是被synchronized修饰的方法,可以保证线程安全。

由jvm创建的main方法线程和system组线程,并不会通过start来启动。

关于多线程start()方法原理解读的更多相关文章

  1. Java线程池原理解读

    引言 引用自<阿里巴巴JAVA开发手册> [强制]线程资源必须通过线程池提供,不允许在应用中自行显式创建线程. 说明:使用线程池的好处是减少在创建和销毁线程上所消耗的时间以及系统资源的开销 ...

  2. Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net

    Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net 1. 提升单例有能力的1 2. 减少工作数量2 2.1. 减少距 ...

  3. 详细分析 Java 中实现多线程的方法有几种?(从本质上出发)

    详细分析 Java 中实现多线程的方法有几种?(从本质上出发) 正确的说法(从本质上出发) 实现多线程的官方正确方法: 2 种. Oracle 官网的文档说明 方法小结 方法一: 实现 Runnabl ...

  4. Java并发之AQS原理解读(三)

    上一篇:Java并发之AQS原理解读(二) 前言 本文从源码角度分析AQS共享锁工作原理,并介绍下使用共享锁的子类如何工作的. 共享锁工作原理 共享锁与独占锁的不同之处在于,获取锁和释放锁成功后,都会 ...

  5. Java并发之AQS原理解读(二)

    上一篇: Java并发之AQS原理解读(一) 前言 本文从源码角度分析AQS独占锁工作原理,并介绍ReentranLock如何应用. 独占锁工作原理 独占锁即每次只有一个线程可以获得同一个锁资源. 获 ...

  6. Java并发之AQS原理解读(一)

    前言 本文简要介绍AQS以及其中两个重要概念:state和Node. AQS 抽象队列同步器AQS是java.util.concurrent.locks包下比较核心的类之一,包括AbstractQue ...

  7. 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析

    MyDisruptor V4版本介绍 在v3版本的MyDisruptor实现多线程消费者后.按照计划,v4版本的MyDisruptor需要支持线程安全的多线程生产者功能. 由于该文属于系列博客的一部分 ...

  8. C# 多线程限制方法调用(monitor)

    多线程执行方法 改方法没有执行完时 别的方法不能调用次方法.用循环执行一个方法可以需要一分钟 在这一分钟只内任何 成员都不能再调用该方法. class MonitorSample { ; //生产者和 ...

  9. Java并发编程(一) 两种实现多线程的方法(Thread,Runnable)

    Java中实现多线程的方法有两种: 继承Thread类和实现Runnable方法,并重写Run方法,然后调用start()方法启动线程.使用Runnable会比Thread要好很多,主要是以下三个原因 ...

随机推荐

  1. FFmpeg(七)音频的播放

    一.Open SL ES播放声音流程 简单说明 Open SL ES是android内部的接口,本身可以解码音频,但是我们用FFmpeg,,也可以来录音 . SL引擎:上下文 混音器:两路声音的混合 ...

  2. vue运行报错webpack-dev-server: command not found

    翻译过来就是: 'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序 解决方法: 然后总结下成功的步骤: 1. 直接在项目目录下: cnpm install npm run ...

  3. Jmeter Json List Element Assertion使用详解

    使用背景: jmeter4.0本身提供json Assertion断言,但当我们想要对返回的json list中的多个字段进行断言的时候,我们就会感到很无力.那么此时我们就可以通过Json List ...

  4. Spring Boot 定时任务 @Scheduled

    项目开发中经常需要执行一些定时任务,比如在每天凌晨,需要从 implala 数据库拉取产品功能活跃数据,分析处理后存入到 MySQL 数据库中.类似这样的需求还有许多,那么怎么去实现定时任务呢,有以下 ...

  5. B站自动刷弹幕

    B站自动填弹幕(附带createEvent消息机制) 昨晚看的比赛真的要气死我.RNG 居然又输了... 为了LPL...我写了一个为LPL加油的脚本.希望大家能和我一起为LPL加油! 脚本代码如下: ...

  6. mac下编译openjdk8?so easy!

    写在最前面的话:建议第一次编译jdk的朋友,查阅任何博客都不如认真读一遍源码中的README-builds.html 环境:macOS High Sierra(10.13.5) 编译jdk:openj ...

  7. 《java编程思想》P125-P140(第七章复用类部分)

    1.类的成员默认的是包访问权限.允许包内成员访问 2.super.scrub() 调用基类的scrub方法 3.继承并不是复制基类的接口.当创建了一个导出类(子类)对象时,该对象包含了一个基类的子对象 ...

  8. Windows 7 SP1官方原版ISO系统镜像所有版本下载集合

    ========================================================================== Windows 7 With SP1 32位简体中 ...

  9. Oier们的幸运数字

    题目描述 JerryC对数字痴迷到了一种非正常的境界.每天JerryC都有喜欢的一些数字.第 iii 天JerryC就喜欢Ai−BiA_i-B_iAi​−Bi​中的数字.但是他觉得这样并不是很有趣,于 ...

  10. 10.Nginx流行架构LNMP

    1.什么是LNMP架构 LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写.L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以 ...