不允许使用java方式启动_细品 Java 中启动线程的正确和错误方式
細(xì)品 Java 中啟動(dòng)線程的正確和錯(cuò)誤方式
前文回顧詳細(xì)分析 Java 中實(shí)現(xiàn)多線程的方法有幾種?(從本質(zhì)上出發(fā))
start 方法和 run 方法的比較
代碼演示:/**
*
* start() 和 run() 的比較
*
*
* @author 踏雪彡尋梅
* @version 1.0
* @date 2020/9/20 - 16:15
* @since JDK1.8
*/public class StartAndRunMethod { public static void main(String[] args) { // run 方法演示
// 輸出: name: main
// 說(shuō)明由主線程去執(zhí)行的, 不符合新建一個(gè)線程的本意
Runnable runnable = () -> {
System.out.println("name: " + Thread.currentThread().getName());
};
runnable.run(); // start 方法演示
// 輸出: name: Thread-0
// 說(shuō)明新建了一個(gè)線程, 符合本意
new Thread(runnable).start();
}
}復(fù)制代碼
從以上示例可以分析出以下兩點(diǎn):直接使用 run 方法不會(huì)啟動(dòng)一個(gè)新線程。(錯(cuò)誤方式)
start 方法會(huì)啟動(dòng)一個(gè)新線程。(正確方式)
start 方法分析
start 方法的含義以及注意事項(xiàng)start 方法可以啟動(dòng)一個(gè)新線程。線程對(duì)象在初始化之后調(diào)用了 start 方法之后, 當(dāng)前線程(通常是主線程)會(huì)請(qǐng)求 JVM 虛擬機(jī)如果有空閑的話來(lái)啟動(dòng)一下這邊的這個(gè)新線程。
也就是說(shuō), 啟動(dòng)一個(gè)新線程的本質(zhì)就是請(qǐng)求 JVM 來(lái)運(yùn)行這個(gè)線程。
至于這個(gè)線程何時(shí)能夠運(yùn)行,并不是簡(jiǎn)單的由我們能夠決定的,而是由線程調(diào)度器去決定的。
如果它很忙,即使我們運(yùn)行了 start 方法,也不一定能夠立刻的啟動(dòng)線程。
所以說(shuō) srtart 方法調(diào)用之后,并不意味這個(gè)方法已經(jīng)開始運(yùn)行了。它可能稍后才會(huì)運(yùn)行,也很有可能很長(zhǎng)時(shí)間都不會(huì)運(yùn)行,比如說(shuō)遇到了饑餓的情況。
這也就印證了有些情況下,線程 1 先掉用了 start 方法,而線程 2 后調(diào)用了 start 方法,卻發(fā)現(xiàn)線程 2 先執(zhí)行線程 1 后執(zhí)行的情況。
總結(jié): 調(diào)用 start 方法的順序并不能決定真正線程執(zhí)行的順序。
注意事項(xiàng)start 方法會(huì)牽扯到兩個(gè)線程。
第一個(gè)就是主線程,因?yàn)槲覀儽仨氁幸粋€(gè)主線程或者是其他的線程(哪怕不是主線程)來(lái)執(zhí)行這個(gè) start 方法,第二個(gè)才是新的線程。
很多情況下會(huì)忽略掉為我們創(chuàng)建線程的這個(gè)主線程,不要誤以為調(diào)用了 start 就已經(jīng)是子線程去執(zhí)行了,這個(gè)語(yǔ)句其實(shí)是主線程或者說(shuō)是父線程來(lái)執(zhí)行的,被執(zhí)行之后才去創(chuàng)建新線程。
start 方法創(chuàng)建新線程的準(zhǔn)備工作首先,它會(huì)讓自己處于就緒狀態(tài)。就緒狀態(tài)指已經(jīng)獲取到除了 CPU 以外的其他資源, 如已經(jīng)設(shè)置了上下文、棧、線程狀態(tài)以及 PC(PC 是一個(gè)寄存器,PC 指向程序運(yùn)行的位置) 等。
做完這些準(zhǔn)備工作之后,就萬(wàn)事俱備只欠東風(fēng)了,東風(fēng)就是 CPU 資源。
做完準(zhǔn)備工作之后,線程才能被 JVM 或操作系統(tǒng)進(jìn)一步去調(diào)度到執(zhí)行狀態(tài)等待獲取 CPU 資源,然后才會(huì)真正地進(jìn)入到運(yùn)行狀態(tài)執(zhí)行 run 方法中的代碼。
需要注意: 不能重復(fù)的執(zhí)行 start 方法代碼示例/**
*
* 演示不能重復(fù)的執(zhí)行 start 方法(兩次及以上), 否則會(huì)報(bào)錯(cuò)
*
*
* @author 踏雪彡尋梅
* @version 1.0
* @date 2020/9/20 - 16:47
* @since JDK1.8
*/public class CantStartTwice { public static void main(String[] args) {
Runnable runnable = () -> {
System.out.println("name: " + Thread.currentThread().getName());
};
Thread thread = new Thread(runnable); // 輸出: name: Thread-0
thread.start(); // 輸出: 拋出 java.lang.IllegalThreadStateException
// 即非法線程狀態(tài)異常(線程狀態(tài)不符合規(guī)定)
thread.start();
}
}復(fù)制代碼
報(bào)錯(cuò)的原因start 一旦開始執(zhí)行,線程狀態(tài)就從最開始的 New 狀態(tài)進(jìn)入到后續(xù)的狀態(tài),比如說(shuō) Runnable,然后一旦線程執(zhí)行完畢,線程就會(huì)變成終止?fàn)顟B(tài),而終止?fàn)顟B(tài)永遠(yuǎn)不可能再返回回去,所以會(huì)拋出以上異常,也就是說(shuō)不能回到初始狀態(tài)了。這里描述的還不夠清晰,讓我們來(lái)看看源碼能了解的更透徹。
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".
*/
// 第一步, 檢查線程狀態(tài)是否為初始狀態(tài), 這里也就是上面拋出異常的原因
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 { // 第三步, 調(diào)用 start0 方法
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 */
}
}
}復(fù)制代碼
源碼中的流程
第一步:啟動(dòng)新線程時(shí)會(huì)首先檢查線程狀態(tài)是否為初始狀態(tài), 這也是以上拋出異常的原因。即以下代碼:if (threadStatus != 0) throw new IllegalThreadStateException();復(fù)制代碼
其中 threadStatus 這個(gè)變量的注釋如下,也就是說(shuō) Java 的線程狀態(tài)最初始(還沒(méi)有啟動(dòng))的時(shí)候表示為 0:/* Java thread status for tools,
* initialized to indicate thread 'not yet started'
*/private volatile int threadStatus = 0;復(fù)制代碼
第二步:將其加入線程組。即以下代碼:group.add(this);復(fù)制代碼
第三步:最后調(diào)用 start0() 這個(gè) native 方法(native 代表它的代碼不是由 Java 實(shí)現(xiàn)的,而是由 C/C++ 實(shí)現(xiàn)的,具體實(shí)現(xiàn)可以在 JDK 里面看到,了解即可), 即以下代碼:boolean started = false;try { // 第三步, 調(diào)用 start0 方法
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 */
}
}復(fù)制代碼
run 方法分析
run 方法源碼分析@Overridepublic void run() { // 傳入了 target 對(duì)象(即 Runnable 接口的實(shí)現(xiàn)), 執(zhí)行傳入的 target 對(duì)象的 run 方法
if (target != null) {
target.run();
}
}復(fù)制代碼
對(duì)于 run 方法的兩種情況第一種: 重寫了 Thread 類的 run 方法,Thread 的 run 方法會(huì)失效, 將會(huì)執(zhí)行重寫的 run 方法。
第二種: 傳入了 target 對(duì)象(即 Runnable 接口的實(shí)現(xiàn)),執(zhí)行 Thread 的原有 run 方法然后接著執(zhí)行 target 對(duì)象的 run 方法。
總結(jié):run 方法就是一個(gè)普通的方法, 上文中直接去執(zhí)行 run 方法也就是相當(dāng)于我們執(zhí)行自己寫的普通方法一樣,所以它的執(zhí)行線程就是我們的主線程。
所以要想真正的啟動(dòng)線程,不能直接調(diào)用 run 方法,而是要調(diào)用 start 方法,其中可以間接的調(diào)用 run 方法。相關(guān)學(xué)習(xí)推薦:java基礎(chǔ)
總結(jié)
以上是生活随笔為你收集整理的不允许使用java方式启动_细品 Java 中启动线程的正确和错误方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: XSS常见payload指的是什么
- 下一篇: java菜单如何点解_【Java】详解菜