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),只有等待另外線程的通知或被中斷才會(huì)返回,需要注意的是調(diào)用wait()方法后,會(huì)釋放對(duì)象的鎖。
因此,wait方法一般用在同步方法或同步代碼塊中。
sleep
sleep導(dǎo)致當(dāng)前線程休眠,與wait方法不同的是sleep不會(huì)釋放當(dāng)前占有的鎖,sleep(long)會(huì)導(dǎo)致線程進(jìn)入TIMED-WATING狀態(tài),而wait()方法會(huì)導(dǎo)致當(dāng)前線程進(jìn)入WATING狀態(tài)
yield
yield會(huì)使當(dāng)前線程讓出CPU執(zhí)行時(shí)間片,與其他線程一起重新競(jìng)爭(zhēng)CPU時(shí)間片。一般情況下,優(yōu)先級(jí)高的線程有更大的可能性成功競(jìng)爭(zhēng)得到CPU時(shí)間片,但這又不是絕對(duì)的,有的操作系統(tǒng)對(duì)線程優(yōu)先級(jí)并不敏感。
interrupt
中斷一個(gè)線程,其本意是給這個(gè)線程一個(gè)通知信號(hào),會(huì)影響這個(gè)線程內(nèi)部的一個(gè)中斷標(biāo)識(shí)位。這個(gè)線程本身并不會(huì)因此而改變狀態(tài)(如阻塞,終止等)。
1.調(diào)用interrupt()方法并不會(huì)中斷一個(gè)正在運(yùn)行的線程。也就是說(shuō)處于Running狀態(tài)的線程并不會(huì)因?yàn)楸恢袛喽唤K止,僅僅改變了內(nèi)部維護(hù)的中斷標(biāo)識(shí)位而已。
2.若調(diào)用sleep()而使線程處于TIMED-WATING狀態(tài),這時(shí)調(diào)用interrupt()方法,會(huì)拋出InterruptedException,從而使線程提前結(jié)束TIMED-WATING狀態(tài)。
3.許多聲明拋出InterruptedException的方法(如Thread.sleep(long mills方法)),拋出異常前,都會(huì)清除中斷標(biāo)識(shí)位,所以拋出異常后,調(diào)用isInterrupted()方法將會(huì)返回false。
4.中斷狀態(tài)是線程固有的一個(gè)標(biāo)識(shí)位,可以通過(guò)此標(biāo)識(shí)位安全的終止線程。比如,你想終止一個(gè)線程thread的時(shí)候,可以調(diào)用thread.interrupt()方法,在線程的run方法內(nèi)部可以根據(jù)thread.isInterrupted()的值來(lái)優(yōu)雅的終止線程。當(dāng)然,你可以在線程內(nèi)部自己維護(hù)一個(gè)boolean變量來(lái)控制線程的運(yùn)行和終止。
現(xiàn)在,我們看一下源碼里這個(gè)方法是怎么說(shuō)明的。
/** * Interrupts this thread. * 中斷這個(gè)線程 * * <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)常是被允許的。不過(guò)要檢查安全性, * 可能會(huì)拋出安全異常的。 * * <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)用一個(gè)Object對(duì)象的多個(gè)wait方法或者當(dāng)前對(duì)象的 * join,sleep方法而正處于阻塞狀態(tài)(僅表示沒(méi)有獲得CPU的時(shí)間片執(zhí)行,不表示 * 線程的BLOCKED狀態(tài))。那么它的中斷狀態(tài)將被清除(復(fù)位),而且它會(huì)收到中 * 斷異常。 * * <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> * 如果上述的條件都沒(méi)有比中的話,那么這個(gè)線程的中斷標(biāo)志位將被設(shè)置。 * * <p> Interrupting a thread that is not alive need not have any effect. * 中斷一個(gè)不存活的線程(未啟動(dòng)或已結(jié)束)不會(huì)有任何影響 * * @throws SecurityException * if the current thread cannot modify this thread * * @revised 6.0 * @spec JSR-51 */ public void interrupt() { //檢查當(dāng)前線程對(duì)this線程的安全權(quán)限,如果不允許修改,會(huì)拋出異常 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)識(shí)位,本地方法 }
基本很簡(jiǎn)單,首先檢查當(dāng)前線程對(duì)this線程的安全權(quán)限,如果不允許修改,會(huì)拋出異常。隨后加鎖同步設(shè)置中斷標(biāo)識(shí)位。盡管方法聲明中有詳細(xì)說(shuō)明,但是代碼中看不出來(lái),處于wating狀態(tài)的線程被中斷后,中斷標(biāo)識(shí)會(huì)復(fù)位。我認(rèn)為這是靠本地代碼interrupt0實(shí)現(xiàn)的。
join
在線程A上下文中執(zhí)行了線程B.join()語(yǔ)句,其含義是線程B執(zhí)行結(jié)束后,join()方法才會(huì)返回,線程A才可繼續(xù)執(zhí)行。
舉個(gè)例子,如果你創(chuàng)建了10個(gè)線程,同時(shí)調(diào)用start()方法執(zhí)行線程,但是你想讓它們有序執(zhí)行,就可以使用join來(lái)輔助完成。代碼如下:
/*** * 此示例中10個(gè)線程在執(zhí)行時(shí),,需要等待前一個(gè)線程執(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中線程的基本方法使用技巧,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
解決idea2020.1找不到程序包和符號(hào)的問(wèn)題
這篇文章主要介紹了解決idea2020.1找不到程序包和符號(hào)的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01java項(xiàng)目導(dǎo)出為.exe執(zhí)行文件的方法步驟
最近做了個(gè)項(xiàng)目,想要轉(zhuǎn)換成可執(zhí)行文件,那么java項(xiàng)目如何導(dǎo)出為.exe執(zhí)行文件,本文就介紹一下,主要使用jar2exe軟件,感興趣的可以了解一下2021-05-05Java面試Logback打印日志如何獲取當(dāng)前方法名稱題解
這篇文章主要為大家介紹了Java面試Logback打印日志如何獲取當(dāng)前方法名稱題解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11關(guān)于Java繼承中父類和子類構(gòu)造函數(shù)的問(wèn)題
這篇文章主要介紹了關(guān)于Java繼承中父類和子類構(gòu)造函數(shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10java創(chuàng)建子類對(duì)象設(shè)置并調(diào)用父類的變量操作
這篇文章主要介紹了java創(chuàng)建子類對(duì)象設(shè)置并調(diào)用父類的變量操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01Spring Cloud Feign實(shí)現(xiàn)文件上傳下載的示例代碼
Feign框架對(duì)于文件上傳消息體格式并沒(méi)有做原生支持,需要集成模塊feign-form來(lái)實(shí)現(xiàn),本文就詳細(xì)的介紹一下如何使用,感興趣的可以了解一下2022-02-02