Java中線程的基本方法使用技巧
java中線程的基本方法的熟練使用是精通多線程編程的必經(jīng)之路,線程相關(guān)的基本方法有wait,notify,notifyAll,sleep,join,yield等,本文淺要的介紹一下它們的使用方式。
線程的狀態(tài)圖

java將操作系統(tǒng)中的就緒和運(yùn)行兩種狀態(tài)統(tǒng)稱為可運(yùn)行狀態(tài),java中線程的狀態(tài)可以認(rèn)為有以上六種。
wait
調(diào)用該方法的線程進(jìn)入WAITING狀態(tài),只有等待另外線程的通知或被中斷才會返回,需要注意的是調(diào)用wait()方法后,會釋放對象的鎖。
因此,wait方法一般用在同步方法或同步代碼塊中。
sleep
sleep導(dǎo)致當(dāng)前線程休眠,與wait方法不同的是sleep不會釋放當(dāng)前占有的鎖,sleep(long)會導(dǎo)致線程進(jìn)入TIMED-WATING狀態(tài),而wait()方法會導(dǎo)致當(dāng)前線程進(jìn)入WATING狀態(tài)
yield
yield會使當(dāng)前線程讓出CPU執(zhí)行時間片,與其他線程一起重新競爭CPU時間片。一般情況下,優(yōu)先級高的線程有更大的可能性成功競爭得到CPU時間片,但這又不是絕對的,有的操作系統(tǒng)對線程優(yōu)先級并不敏感。
interrupt
中斷一個線程,其本意是給這個線程一個通知信號,會影響這個線程內(nèi)部的一個中斷標(biāo)識位。這個線程本身并不會因此而改變狀態(tài)(如阻塞,終止等)。
1.調(diào)用interrupt()方法并不會中斷一個正在運(yùn)行的線程。也就是說處于Running狀態(tài)的線程并不會因為被中斷而被終止,僅僅改變了內(nèi)部維護(hù)的中斷標(biāo)識位而已。
2.若調(diào)用sleep()而使線程處于TIMED-WATING狀態(tài),這時調(diào)用interrupt()方法,會拋出InterruptedException,從而使線程提前結(jié)束TIMED-WATING狀態(tài)。
3.許多聲明拋出InterruptedException的方法(如Thread.sleep(long mills方法)),拋出異常前,都會清除中斷標(biāo)識位,所以拋出異常后,調(diào)用isInterrupted()方法將會返回false。
4.中斷狀態(tài)是線程固有的一個標(biāo)識位,可以通過此標(biāo)識位安全的終止線程。比如,你想終止一個線程thread的時候,可以調(diào)用thread.interrupt()方法,在線程的run方法內(nèi)部可以根據(jù)thread.isInterrupted()的值來優(yōu)雅的終止線程。當(dāng)然,你可以在線程內(nèi)部自己維護(hù)一個boolean變量來控制線程的運(yùn)行和終止。
現(xiàn)在,我們看一下源碼里這個方法是怎么說明的。
/**
* Interrupts this thread.
* 中斷這個線程
*
* <p> Unless the current thread is interrupting itself, which is
* always permitted, the {@link #checkAccess() checkAccess} method
* of this thread is invoked, which may cause a {@link
* SecurityException} to be thrown.
* 如果不是當(dāng)前線程中斷自身,這經(jīng)常是被允許的。不過要檢查安全性,
* 可能會拋出安全異常的。
*
* <p> If this thread is blocked in an invocation of the {@link
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.
* 如果中斷的線程由于調(diào)用一個Object對象的多個wait方法或者當(dāng)前對象的
* join,sleep方法而正處于阻塞狀態(tài)(僅表示沒有獲得CPU的時間片執(zhí)行,不表示
* 線程的BLOCKED狀態(tài))。那么它的中斷狀態(tài)將被清除(復(fù)位),而且它會收到中
* 斷異常。
*
* <p> If this thread is blocked in an I/O operation upon an {@link
* java.nio.channels.InterruptibleChannel InterruptibleChannel}
* then the channel will be closed, the thread's interrupt
* status will be set, and the thread will receive a {@link
* java.nio.channels.ClosedByInterruptException}.
*
* <p> If this thread is blocked in a {@link java.nio.channels.Selector}
* then the thread's interrupt status will be set and it will return
* immediately from the selection operation, possibly with a non-zero
* value, just as if the selector's {@link
* java.nio.channels.Selector#wakeup wakeup} method were invoked.
*
* <p> If none of the previous conditions hold then this thread's interrupt
* status will be set. </p>
* 如果上述的條件都沒有比中的話,那么這個線程的中斷標(biāo)志位將被設(shè)置。
*
* <p> Interrupting a thread that is not alive need not have any effect.
* 中斷一個不存活的線程(未啟動或已結(jié)束)不會有任何影響
*
* @throws SecurityException
* if the current thread cannot modify this thread
*
* @revised 6.0
* @spec JSR-51
*/
public void interrupt() {
//檢查當(dāng)前線程對this線程的安全權(quán)限,如果不允許修改,會拋出異常
if (this != Thread.currentThread())
checkAccess();
//加鎖同步
synchronized (blockerLock) {
Interruptible b = blocker;
if (b != null) {
interrupt0(); // Just to set the interrupt flag
b.interrupt(this);
return;
}
}
interrupt0(); //設(shè)置標(biāo)識位,本地方法
}
基本很簡單,首先檢查當(dāng)前線程對this線程的安全權(quán)限,如果不允許修改,會拋出異常。隨后加鎖同步設(shè)置中斷標(biāo)識位。盡管方法聲明中有詳細(xì)說明,但是代碼中看不出來,處于wating狀態(tài)的線程被中斷后,中斷標(biāo)識會復(fù)位。我認(rèn)為這是靠本地代碼interrupt0實現(xiàn)的。
join
在線程A上下文中執(zhí)行了線程B.join()語句,其含義是線程B執(zhí)行結(jié)束后,join()方法才會返回,線程A才可繼續(xù)執(zhí)行。
舉個例子,如果你創(chuàng)建了10個線程,同時調(diào)用start()方法執(zhí)行線程,但是你想讓它們有序執(zhí)行,就可以使用join來輔助完成。代碼如下:
/***
* 此示例中10個線程在執(zhí)行時,,需要等待前一個線程執(zhí)行完;
* 比如線程0要等待main線程執(zhí)行完
* 線程9要等到線程8執(zhí)行完
*
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Thread previous = Thread.currentThread();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Dimon(previous), String.valueOf(i));
thread.start();
previous = thread;
}
TimeUnit.SECONDS.sleep(5);
System.out.println(Thread.currentThread().getName() + "terminate");
}
static class Dimon implements Runnable{
private Thread thread;
public Dimon(Thread thread){
this.thread = thread;
}
@Override
public void run() {
try {
thread.join();
}catch (Exception e){
}
System.out.println( Thread.currentThread().getName() + ":terminate" );
}
}
總結(jié)
以上所述是小編給大家介紹的Java中線程的基本方法使用技巧,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java有趣好玩的圖形界面開發(fā)八個案例實現(xiàn)
今天使用GUI技術(shù)寫了幾個練習(xí)的Demo,希望對大家學(xué)習(xí)圖形用戶界面有所幫助,感興趣的同學(xué)來看看吧,動手敲一遍理解更通透2022-05-05
解決springboot+activemq啟動報注解錯誤的問題
這篇文章主要介紹了解決springboot+activemq啟動報注解錯誤的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
零基礎(chǔ)學(xué)Java:Java開發(fā)工具 Eclipse 安裝過程創(chuàng)建第一個Java項目及Eclipse的一些基礎(chǔ)使用技巧
這篇文章主要介紹了零基礎(chǔ)學(xué)Java:Java開發(fā)工具 Eclipse 安裝過程創(chuàng)建第一個Java項目及Eclipse的一些基礎(chǔ)使用技巧,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
解決mybatis 執(zhí)行mapper的方法時報空指針問題
這篇文章主要介紹了解決mybatis 執(zhí)行mapper的方法時報空指針問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot實現(xiàn)接口統(tǒng)一前綴
本文主要介紹了SpringBoot實現(xiàn)接口統(tǒng)一前綴,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

