1.start()和run()的比较
package threadcoreknowledge.startthread;
/**
* 描述: 对比start和run两种启动线程的方式
*/
public class StartAndRunMethod {
public static void main(String[] args) {
Runnable runnable =() -> {
System.out.println(Thread.currentThread().getName());
};
//调用run方法
runnable.run();
//调用start方法
new Thread(runnable).start();
}
}
运行结果
E:\.jdks\corretto-1.8.0_252\bin\java.exe "-javaagent:E:\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar=50347:E:\IntelliJ IDEA 2019.3.5\bin" -Dfile.encoding=UTF-8 -classpath E:\.jdks\corretto-1.8.0_252\jre\lib\charsets.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\access-bridge-64.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\cldrdata.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\dnsns.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\jaccess.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\jfxrt.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\localedata.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\nashorn.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\sunec.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\sunjce_provider.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\sunmscapi.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\sunpkcs11.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\zipfs.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\jce.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\jfxswt.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\jsse.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\management-agent.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\resources.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\rt.jar;E:\workspace\thread-study\out\production\demo01 threadcoreknowledge.startthread.StartAndRunMethod
main
Thread-0
Process finished with exit code 0
2.start()方法原理解读
2.1 方法意义
1. 启动新线程:当前线程调用start()方法,通知JVM在有空闲的情况下启动新线程
2. 准备工作: 子线程需要准备工作,将自己处于就绪状态(获取了除了CPU以外外的所有资源)
3. 不能重复执行start(): 重复执行start()方法的代码和结果如下
package threadcoreknowledge.startthread;
/**
* 描述: 演示不能两次调用start方法,否则会报错
*/
public class CantStartTwice {
public static void main(String[] args) {
Thread thread = new Thread();
thread.start();
thread.start();
}
}
E:\.jdks\corretto-1.8.0_252\bin\java.exe "-javaagent:E:\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar=50799:E:\IntelliJ IDEA 2019.3.5\bin" -Dfile.encoding=UTF-8 -classpath E:\.jdks\corretto-1.8.0_252\jre\lib\charsets.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\access-bridge-64.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\cldrdata.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\dnsns.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\jaccess.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\jfxrt.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\localedata.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\nashorn.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\sunec.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\sunjce_provider.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\sunmscapi.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\sunpkcs11.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\ext\zipfs.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\jce.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\jfxswt.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\jsse.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\management-agent.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\resources.jar;E:\.jdks\corretto-1.8.0_252\jre\lib\rt.jar;E:\workspace\thread-study\out\production\demo01 threadcoreknowledge.startthread.CantStartTwice
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at threadcoreknowledge.startthread.CantStartTwice.main(CantStartTwice.java:10)
Process finished with exit code 1
2.2 源码解析
1.启动新线程检查线程状态
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
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 */
}
}
}
private native void start0();
从源码可以知道,调用start()时,会判断threadStatus !=0,很明显,刚刚的异常就是这个地方抛出来的,通过源码可以知道threadStatus是记录线程的状态,初始默认为0,当线程启动后,该线程的状态就改变了,所以threadStatus的值也就改变了,当第二次调用start()时,就会抛出异常。
————————————————
原文链接:https://kaven.blog.csdn.net/article/details/104314401
2.加入线程组:group.add(this);
3.调用start0():private native void start0();start0方法为native方法,是由C++实现的。
3.run()方法原理解读
3.1源码分析
@Override
public void run() {
if (target != null) {
target.run();
}
}
3.2两种情况
1.重写了Thread类的run方法
2.传入了target对象
run()只是一个类中的普通方法,调用run()跟调用普通方法一样。
而调用start(),则会做一系列工作去创建新线程,然后在新线程中执行run()里面的任务内容。
————————————————
原文链接:https://kaven.blog.csdn.net/article/details/104314401
总结:常见面试问题
1.一个线程两次调用start()方法会出现什么情况?为什么?
会抛出IllegalThreadStateException异常。在start()方法开始的时候就会对线程的状态进行检查,如果运行状态不是0,则会抛出异常。(补充6个状态后面学)
2.既然start()方法会调用run()方法,为什么选择调用start()方法,而不是直接调用run()方法呢?
run()只是一个类中的普通方法,调用run()跟调用普通方法一样。而调用start(),则会做一系列工作去创建新线程,然后在新线程中执行run()里面的任务内容。