java基本教程之Thread中start()和run()的區(qū)別 java多線程教程
Thread類包含start()和run()方法,它們的區(qū)別是什么?本章將對(duì)此作出解答。本章內(nèi)容包括:
start() 和 run()的區(qū)別說明
start() 和 run()的區(qū)別示例
start() 和 run()相關(guān)源碼(基于JDK1.7.0_40)
start() 和 run()的區(qū)別說明
start() : 它的作用是啟動(dòng)一個(gè)新線程,新線程會(huì)執(zhí)行相應(yīng)的run()方法。start()不能被重復(fù)調(diào)用。
run() : run()就和普通的成員方法一樣,可以被重復(fù)調(diào)用。單獨(dú)調(diào)用run()的話,會(huì)在當(dāng)前線程中執(zhí)行run(),而并不會(huì)啟動(dòng)新線程!
下面以代碼來進(jìn)行說明。
class MyThread extends Thread{
public void run(){
...
}
};
MyThread mythread = new MyThread();
mythread.start()會(huì)啟動(dòng)一個(gè)新線程,并在新線程中運(yùn)行run()方法。
而mythread.run()則會(huì)直接在當(dāng)前線程中運(yùn)行run()方法,并不會(huì)啟動(dòng)一個(gè)新線程來運(yùn)行run()。
start() 和 run()的區(qū)別示例
下面,通過一個(gè)簡(jiǎn)單示例演示它們之間的區(qū)別。源碼如下:
public synchronized void start() {
// 如果線程不是"就緒狀態(tài)",則拋出異常!
if (threadStatus != 0)
throw new IllegalThreadStateException();
// 將線程添加到ThreadGroup中
group.add(this);
boolean started = false;
try {
// 通過start0()啟動(dòng)線程
start0();
// 設(shè)置started標(biāo)記
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
}
}
}
運(yùn)行結(jié)果:
main call mythread.run()
main is running
main call mythread.start()
mythread is running
結(jié)果說明:
(01) Thread.currentThread().getName()是用于獲取“當(dāng)前線程”的名字。當(dāng)前線程是指正在cpu中調(diào)度執(zhí)行的線程。
(02) mythread.run()是在“主線程main”中調(diào)用的,該run()方法直接運(yùn)行在“主線程main”上。
(03) mythread.start()會(huì)啟動(dòng)“線程mythread”,“線程mythread”啟動(dòng)之后,會(huì)調(diào)用run()方法;此時(shí)的run()方法是運(yùn)行在“線程mythread”上。
start() 和 run()相關(guān)源碼(基于JDK1.7.0_40)
Thread.java中start()方法的源碼如下:
public synchronized void start() {
// 如果線程不是"就緒狀態(tài)",則拋出異常!
if (threadStatus != 0)
throw new IllegalThreadStateException();
// 將線程添加到ThreadGroup中
group.add(this);
boolean started = false;
try {
// 通過start0()啟動(dòng)線程
start0();
// 設(shè)置started標(biāo)記
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
}
}
}
說明:start()實(shí)際上是通過本地方法start0()啟動(dòng)線程的。而start0()會(huì)新運(yùn)行一個(gè)線程,新線程會(huì)調(diào)用run()方法。
private native void start0();
Thread.java中run()的代碼如下:
public void run() {
if (target != null) {
target.run();
}
}
說明:target是一個(gè)Runnable對(duì)象。run()就是直接調(diào)用Thread線程的Runnable成員的run()方法,并不會(huì)新建一個(gè)線程。
相關(guān)文章
Java Switch對(duì)各類型支持實(shí)現(xiàn)原理
這篇文章主要介紹了Java Switch對(duì)各類型支持實(shí)現(xiàn)原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05解決SpringBoot中的Scheduled單線程執(zhí)行問題
在一次SpringBoot中使用Scheduled定時(shí)任務(wù)時(shí),發(fā)現(xiàn)某一個(gè)任務(wù)出現(xiàn)執(zhí)行占用大量資源,會(huì)導(dǎo)致其他任務(wù)也執(zhí)行失敗,這篇文章主要介紹了SpringBoot中的Scheduled單線程執(zhí)行問題及解決方法,需要的朋友可以參考下2022-06-06Java生產(chǎn)者和消費(fèi)者例子_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
生產(chǎn)者-消費(fèi)者(producer-consumer)問題,也稱作有界緩沖區(qū)(bounded-buffer)問題,兩個(gè)進(jìn)程共享一個(gè)公共的固定大小的緩沖區(qū)。下文通過實(shí)例給大家介紹java生產(chǎn)者和消費(fèi)者,感興趣的朋友一起學(xué)習(xí)吧2017-05-05Spring Data分頁(yè)與排序的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Spring Data分頁(yè)與排序的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12spring security認(rèn)證異常后返回中文提示的問題
這篇文章主要介紹了spring security認(rèn)證異常后返回中文提示的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

淺談@RequestParam(required = true)的誤區(qū)

關(guān)于Spring啟動(dòng)時(shí)Context加載源碼分析