Thread 变量执行两次 start()方法会发生什么?

247 阅读1分钟

Thread 类中有一个属性 threadStatus,记录了线程状态。默认为 0

start 方法源码如下

public synchornized void start()if(threadStatus != 0)
        throw new IllegalThreadStateException();
    ....
}

第一次调用 start 方法,这个变量会被赋值为 2(为什么是 2 有待考虑)

第二次调用 start 方法,会判断 threadStatus 是不是 0,如果不是 0 代表线程已经启动了。此时应该拒绝执行 start 方法。因此会抛出IllegalThreadStateException异常