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

java將操作系統(tǒng)中的就緒和運行兩種狀態(tài)統(tǒng)稱為可運行狀態(tài),java中線程的狀態(tài)可以認為有以上六種。
wait
調用該方法的線程進入WAITING狀態(tài),只有等待另外線程的通知或被中斷才會返回,需要注意的是調用wait()方法后,會釋放對象的鎖。
因此,wait方法一般用在同步方法或同步代碼塊中。
sleep
sleep導致當前線程休眠,與wait方法不同的是sleep不會釋放當前占有的鎖,sleep(long)會導致線程進入TIMED-WATING狀態(tài),而wait()方法會導致當前線程進入WATING狀態(tài)
yield
yield會使當前線程讓出CPU執(zhí)行時間片,與其他線程一起重新競爭CPU時間片。一般情況下,優(yōu)先級高的線程有更大的可能性成功競爭得到CPU時間片,但這又不是絕對的,有的操作系統(tǒng)對線程優(yōu)先級并不敏感。
interrupt
中斷一個線程,其本意是給這個線程一個通知信號,會影響這個線程內部的一個中斷標識位。這個線程本身并不會因此而改變狀態(tài)(如阻塞,終止等)。
1.調用interrupt()方法并不會中斷一個正在運行的線程。也就是說處于Running狀態(tài)的線程并不會因為被中斷而被終止,僅僅改變了內部維護的中斷標識位而已。
2.若調用sleep()而使線程處于TIMED-WATING狀態(tài),這時調用interrupt()方法,會拋出InterruptedException,從而使線程提前結束TIMED-WATING狀態(tài)。
3.許多聲明拋出InterruptedException的方法(如Thread.sleep(long mills方法)),拋出異常前,都會清除中斷標識位,所以拋出異常后,調用isInterrupted()方法將會返回false。
4.中斷狀態(tài)是線程固有的一個標識位,可以通過此標識位安全的終止線程。比如,你想終止一個線程thread的時候,可以調用thread.interrupt()方法,在線程的run方法內部可以根據(jù)thread.isInterrupted()的值來優(yōu)雅的終止線程。當然,你可以在線程內部自己維護一個boolean變量來控制線程的運行和終止。
現(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.
* 如果不是當前線程中斷自身,這經(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}.
* 如果中斷的線程由于調用一個Object對象的多個wait方法或者當前對象的
* join,sleep方法而正處于阻塞狀態(tài)(僅表示沒有獲得CPU的時間片執(zhí)行,不表示
* 線程的BLOCKED狀態(tài))。那么它的中斷狀態(tài)將被清除(復位),而且它會收到中
* 斷異常。
*
* <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>
* 如果上述的條件都沒有比中的話,那么這個線程的中斷標志位將被設置。
*
* <p> Interrupting a thread that is not alive need not have any effect.
* 中斷一個不存活的線程(未啟動或已結束)不會有任何影響
*
* @throws SecurityException
* if the current thread cannot modify this thread
*
* @revised 6.0
* @spec JSR-51
*/
public void interrupt() {
//檢查當前線程對this線程的安全權限,如果不允許修改,會拋出異常
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(); //設置標識位,本地方法
}
基本很簡單,首先檢查當前線程對this線程的安全權限,如果不允許修改,會拋出異常。隨后加鎖同步設置中斷標識位。盡管方法聲明中有詳細說明,但是代碼中看不出來,處于wating狀態(tài)的線程被中斷后,中斷標識會復位。我認為這是靠本地代碼interrupt0實現(xiàn)的。
join
在線程A上下文中執(zhí)行了線程B.join()語句,其含義是線程B執(zhí)行結束后,join()方法才會返回,線程A才可繼續(xù)執(zhí)行。
舉個例子,如果你創(chuàng)建了10個線程,同時調用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" );
}
}
總結
以上所述是小編給大家介紹的Java中線程的基本方法使用技巧,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
java創(chuàng)建子類對象設置并調用父類的變量操作
這篇文章主要介紹了java創(chuàng)建子類對象設置并調用父類的變量操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Spring Cloud Feign實現(xiàn)文件上傳下載的示例代碼
Feign框架對于文件上傳消息體格式并沒有做原生支持,需要集成模塊feign-form來實現(xiàn),本文就詳細的介紹一下如何使用,感興趣的可以了解一下2022-02-02

