java线程启动原理
start方法和run方法的比较
public class startAndRunMedthod {
public static void main(String[] args) {
Runnable runnable = () -> {
System.out.println(Thread.currentThread().getName());
};
runnable.run();
new Thread(runnable).start();
}
}
- 输出结果
main
Thread-0
// main 是由run方法输出的
// Thread-0是由start方法输出的
start方法的原理
start方法的含义
- 启动新线程
调用start方法只是告诉JVM要启动新线程,具体启动的数据要根据任务调度来决定
调用start的方法需要两个线程来同时运行,因为需要另外一个线程来执行这个start方法
- 准备工作
调用start方法之后,先让自己处于就绪状态(已经取到了除CPU以外的其他资源,设置了上下文,栈,线程状态,PC等)
然后被CPU调度到执行状态之后,等待CPU资源,然后才处于运行状态,执行run方法里面的代码
- 不能重复执行start方法
public class startTwice {
public static void main(String[] args) {
Thread thread = new Thread();
thread.start();
thread.start();
}
}
运行结果
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at com.jx.JavaTest.satrtThread.startTwice.main(startTwice.java:8)
执行start方法之后,线程就从new状态转变成后续的状态,执行完毕,线程就会变成终止状态。
在start的源码中,启动新线程先检查线程状态,然后将线程加入线程组,调用start0方法
- 源码(请看注释)
private volatile int threadStatus = 0;
public synchronized void start() {
// 调用start方法,首先先检查状态参数,这个参数由最上面可见,初始值就为0,表示没有启动
if (threadStatus != 0)
throw new IllegalThreadStateException();
// 加入到线程组
group.add(this);
boolean started = false;
// 执行start0方法
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 */
}
}
}
private native void start0();
run方法的原理
- 源码
@Override
public void run() {
if (target != null) {
target.run();
}
}
run方法就是一个普通方法,和自己写的方法没有区别
要想真正的启动线程,不能去执行run方法,而是要去执行start方法,间接执行run方法