启动线程正确和错误的方式
1.start()
和run()
的比较
//调用run方法
Runnable runnable = () -> {
System.out.println("thread name is " + Thread.currentThread().getName() + ",thread id=" + Thread.currentThread().getId());
};
runnable.run();
//调用start方法
Thread thread = new Thread(runnable);
thread.start();
输出结果:
thread name is main,thread id=1
thread name is Thread-0,thread id=12
结论:从输出结果可知
run()
方法并不能启动一个线程,执行该方法的还是主线程。
2.start()方法原理
解读
线程对象在初始化之后,调用
start
方法,于是当前线程(主线程)执行start
方法,并通知虚拟机执行新线程。start
方法的作用是通知JVM
在有空闲的情况下启动新线程。子线程何时执行是由线程调度器控制的,start
方法调用结束之后并不意味着线程能已经在运行了。调用start
方法的顺序并不能决定执行线程的顺序。start
方法会让两个线程启动一个是主线程(或者是执行子线程的线程),另一个是子线程。
- 准备工作
新建的线程处于就绪状态(已经获取到除了CPU以外的其它资源)
- 不能重复两次执行
start
方法。
public static void main(String[] args) {
Runnable runnable = ()->{
System.out.println("thread name is " + Thread.currentThread().getName() + ",thread id=" + Thread.currentThread().getId());
};
Thread thread = new Thread(runnable);
thread.start();
thread.start();
}
输出结果
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at com.whaleson.threadcoreknowledge.startthread.CantStartTwice.main(CantStartTwice.java:19)
解析:线程一旦开始执行就从最开始的
new
状态进入到后续的状态。一旦线程执行完毕,线程就会变成终止状态,而终止状态永远不可能返回回去。
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 */
}
}
}
3.run()方法原理
解读
public void run() {
if (target != null) {
target.run();
}
}
4.常见面试问题
- 一个线程
两次
调用start()
方法会出现什么情况?为什么? - 既然start()方法会调用run()方法,为什么我们选择调用start()方法,而不是
直接调用run()方法
呢?