从Thread.start()方法看Thread源码,多次start一个线程会怎么样

这篇文章作为Thread类源码剖析的补充,从一个侧面来看Thread源码。也解答了面试高频问题:“多次start一个线程会怎么样?”

答案是:java.lang.IllegalThreadStateException   线程状态非法异常   继承关系是:--->extends IllegalArgumentException--->extends RuntimeException一个运行时异常,下面我们从源码来透彻分析一下start()时做了什么。

 1 /**
 2      * Causes this thread to begin execution; the Java Virtual Machine
 3      * calls the <code>run</code> method of this thread.
 4      * <p>线程被执行,JVM调用run方法
 5      * The result is that two threads are running concurrently: the
 6      * current thread (which returns from the call to the
 7      * <code>start</code> method) and the other thread (which executes its
 8      * <code>run</code> method).
 9      * <p>
10      * It is never legal to start a thread more than once.多次调用start方法启动一个线程是非法的
11      * In particular, a thread may not be restarted once it has completed
12      * execution.
13      *
14      * @exception  IllegalThreadStateException  if the thread was already已经启动的线程再次start,异常
15      *               started.
16      * @see        #run()
17      * @see        #stop()
18      */
19     public synchronized void start() {
20         /**
21          * This method is not invoked for the main method thread or "system"
22          * group threads created/set up by the VM. Any new functionality added
23          * to this method in the future may have to also be added to the VM.
24          *
25          * A zero status value corresponds to state "NEW".
26          */
27         if (threadStatus != 0)//状态校验  0:NEW 新建状态
28             throw new IllegalThreadStateException();
29
30         /* Notify the group that this thread is about to be started
31          * so that it can be added to the group‘s list of threads
32          * and the group‘s unstarted count can be decremented. */
33         group.add(this);//添加进线程组
34
35         boolean started = false;
36         try {
37             start0();//调用native方法执行线程run方法
38             started = true;
39         } finally {
40             try {
41                 if (!started) {
42                     group.threadStartFailed(this);//启动失败,从线程组中移除当前前程。
43                 }
44             } catch (Throwable ignore) {
45                 /* do nothing. If start0 threw a Throwable then
46                   it will be passed up the call stack */
47             }
48         }
49     }
50
51     private native void start0();

greop.add(this),把当前线程添加进线程组,源码如下:

 1 /**
 2      * Adds the specified thread to this thread group.
 3      *
 4      * <p> Note: This method is called from both library code
 5      * and the Virtual Machine. It is called from VM to add
 6      * certain system threads to the system thread group.
 7      *
 8      * @param  t
 9      *         the Thread to be added
10      *
11      * @throws  IllegalThreadStateException
12      *          if the Thread group has been destroyed
13      */
14     void add(Thread t) {
15         synchronized (this) {
16             if (destroyed) {//线程组状态校验
17                 throw new IllegalThreadStateException();
18             }
19             if (threads == null) {
20                 threads = new Thread[4];//初始化长度为4的Thread数组
21             } else if (nthreads == threads.length) {//数组满了就扩容2倍
22                 threads = Arrays.copyOf(threads, nthreads * 2);
23             }
24             threads[nthreads] = t;//新线程t添加进数组
25
26             // This is done last so it doesn‘t matter in case the
27             // thread is killed
28             nthreads++;//线程数加1
29
30             // The thread is now a fully fledged member of the group, even
31             // though it may, or may not, have been started yet. It will prevent
32             // the group from being destroyed so the unstarted Threads count is
33             // decremented.
34             nUnstartedThreads--;//未启动线程数-1
35         }
36     }

启动失败后调用group.threadStartFailed(this),都是加锁方法,从线程组中移除当前线程,源码如下

 1 void threadStartFailed(Thread t) {
 2         synchronized(this) {
 3             remove(t);//移除线程t
 4             nUnstartedThreads++;//未启动线程+1
 5         }
 6     }
 7
 8 private void remove(Thread t) {
 9         synchronized (this) {
10             if (destroyed) {
11                 return;
12             }
13             for (int i = 0 ; i < nthreads ; i++) {
14                 if (threads[i] == t) {
15                     System.arraycopy(threads, i + 1, threads, i, --nthreads - i);
16                     // Zap dangling reference to the dead thread so that
17                     // the garbage collector will collect it.
18                     threads[nthreads] = null;
19                     break;
20                 }
21             }
22         }
23     }
时间: 2024-08-22 05:29:22

从Thread.start()方法看Thread源码,多次start一个线程会怎么样的相关文章

java 集合与数组的互转方法,与源码分析

前言 java数组与集合需要互相转换的场景非常多,但是运用不好还是容易抛出UnSupportedOperationException.下面讲解一下互转的方法,以及结合源码分异常产生的原因 集合转数组 List<String> strList = new ArrayList<>(10); strList.add("aa"); strList.add("bb"); strList.add("cc"); String[] str

边看MHA源码边学Perl语言之二 ManagerUtil

边看MHA源码边学Perl语言之二ManagerUtil.pm MHA版本 为了让大家有一个共同的代码学习环境,特别从网络找了mha4mysql-manager-0.56,mha4mysql-node-0.56稳定版作为学习和研究对象,大家可以到直接到github上进行clone: https://github.com/mysql-dev-fun/mha4mysql-manager-0.56 https://github.com/mysql-dev-fun/mha4mysql-node-0.56

边看MHA源码边学Perl语言之三 NodeUtil.pm

边看MHA源码边学Perl语言之三 NodeUtil.pm NodeUtil.pm源码分析 MHA的代码分为mha4mysql-manager(管理节点)和mha4mysql-node(数据库节点)两部分,可能有些人认为mha4mysql-node只需要安装在数据库节点就可以了,但通过源码可看出,在管理点节也是需要安装node节点,因为在manager节点也会调用NodeUtil中的方法.以下为加过comment的mha4mysql-node的代码: #!/usr/bin/env perl #

带着问题看redux源码

前言 作为前端状态管理器,这个比较跨时代的工具库redux有很多实现和思想值得我们思考.在深入源码之前,我们可以相关注下一些常见问题,这样带着问题去看实现,也能更加清晰的了解. 常见问题 大概看了下主要有这么几个: redux三大原则 这个可以直接参考官方文档 redux 的优缺点. 关于优缺点,太主观了大家见仁见智. redux中间件相关,洋葱模型是什么,常见中间件. 背景 有关acton,reducer相关的部分可以看我前面的文章.我们主要关注针对store和中间件相关的部分来解读. sto

JDK 源码解析 —— Executors ExecutorService ThreadPoolExecutor 线程池

零. 简介 Executors 是 Executor.ExecutorService.ThreadFactory.Callable 类的工厂和工具方法. 一. 源码解析 创建一个固定大小的线程池:通过重用共享无界队列里的线程来减少线程创建的开销.当所有的线程都在执行任务,新增的任务将会在队列中等待,直到一个线程空闲.由于在执行前失败导致的线程中断,如果需要继续执行接下去的任务,新的线程会取代它执行.线程池中的线程会一直存在,除非明确地 shutdown 掉. public static Exec

netty源码分析之揭开reactor线程的面纱(二)

如果你对netty的reactor线程不了解,建议先看下上一篇文章netty源码分析之揭开reactor线程的面纱(一),这里再把reactor中的三个步骤的图贴一下 reactor线程 我们已经了解到netty reactor线程的第一步是轮询出注册在selector上面的IO事件(select),那么接下来就要处理这些IO事件(process selected keys),本篇文章我们将一起来探讨netty处理IO事件的细节 我们进入到reactor线程的 run 方法,找到处理IO事件的代

你与优秀源码之间只差一个 Star

fir.im Weekly - 你与优秀源码之间只差一个 Star 说起开源社区,Github 是一个不可缺少的存在.作为全球最大的同性交友网站,上面有太多优秀的开源代码库和编程大神,让无数开发者心生向往.那么如何正确的使用 Github,也许是编程学习之必要.来看下@googdev 的 关于如何选择开源项目 ,如何正确使用开源项目,如何发现优秀的开源项目,同时作者还出了几篇从0开始学习 GitHub的系列文章,对于迫切需要成长的开发者会有所帮助. 本期fir.im Weekly 收集了不少好的

关于追踪qemu 源码函数路径的一个方法

这阵子一直在研究qemu 磁盘io路径的源码,发现直接看代码是意见非常低效率的事情,qemu是一个比较庞大的家伙(源码部分大概154MB,完全由C语言来完成),整个结构也都非常地复杂,所以从代码上研究qemu最好的办法只有debug之.不断地收集更多的debug信息去获取源码所蕴含的道理. 很多人第一反应可能就是使用一些类似与Eclipse, gdb 这一类强大的debugger,我当然也不例外,在经过一个上午究竟该使用Eclipse还是gdb的思想斗争的私人情绪之后,我才恍然明白,原来我两个工

由自动装箱和拆箱引发我看Integer源码

背景和问题 在看别人整理的资料时,看到如下一段代码: package com.sitech.test; /** * 自动装箱和拆箱 jdk1.6 * @author liaowp * */ public class TestInteger { public static void main(String[] args) { Integer i1 = 80, i2 = 80, i3 = 999, i4 = 999; System.out.println(i1 == i2);//true Syste