對(duì)比start方法與run方法
代碼部分
public class StartAndRunMethod {
public static void main(String[] args) {
Runnable printThreadName=()->{
System.out.println(Thread.currentThread().getName());
};
new Thread(printThreadName).run();//result:main
new Thread(printThreadName).start();//Thread-0
}
}
正確啟動(dòng)一個(gè)線程的方法應(yīng)該是使用start方法搓萧。
對(duì)比start方法與run方法的源碼,如下所示柴我。
- run
/**
* If this thread was constructed using a separate
* <code>Runnable</code> run object, then that
* <code>Runnable</code> object's <code>run</code> method is called;
* otherwise, this method does nothing and returns.
* <p>
* Subclasses of <code>Thread</code> should override this method.
*
* @see #start()
* @see #stop()
* @see #Thread(ThreadGroup, Runnable, String)
*/
@Override
public void run() {
if (target != null) {
target.run();
}
}
- start
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 */
}
}
}
start方法主要關(guān)注try代碼塊中的代碼。從上可以看到如果是執(zhí)行run方法的話,那么就是主線程直接執(zhí)行了那段代碼塊牙言,調(diào)用了target的run方法,所以結(jié)果肯定是Main怪得。而start方法里面有一個(gè)關(guān)鍵函數(shù)調(diào)用start0咱枉,這一句是用來創(chuàng)建線程的卑硫,也就是說必須要先創(chuàng)建線程才行,所以必須要用start方法庞钢。
調(diào)用了start方法不一定就立刻會(huì)創(chuàng)建線程拔恰,這得根據(jù)JVM的調(diào)度算法來決定。
- 不能重復(fù)調(diào)用start基括,否則會(huì)拋出異常java.lang.IllegalThreadStateException颜懊。
再次閱讀start方法的代碼,可以看到start方法分為了三步:- 檢查線程狀態(tài)
- 加入線程組
- 啟動(dòng)線程
在第一步中风皿,如果線程狀態(tài)不為0(線程狀態(tài)為NEW)的話就會(huì)拋出java.lang.IllegalThreadStateException河爹。
另外start方法是被synchronized關(guān)鍵字修飾的,可以保證線程安全桐款,main方法線程和system組的線程則是由JVM創(chuàng)建的咸这,并不會(huì)通過start來啟動(dòng)。