摘要:和是配套使用的,方法容易導致死鎖。方法不會保證線程的資源正常釋放方法給線程打個停止標記,將線程的中斷狀態(tài)設(shè)置為,并沒有馬上強制中斷線程,線程是否中斷由線程自己決定。終結(jié)狀態(tài),還是返回。方法判斷當前線程是否中斷,清除中斷標志。
resume、suspend、stop
resume和suspend是配套使用的,suspend方法容易導致死鎖。
stop方法不會保證線程的資源正常釋放
interruptinterrupt()方法:給線程打個停止標記,將線程的中斷狀態(tài)設(shè)置為true,并沒有馬上強制中斷線程,線程是否中斷由線程自己決定。
isInterrupted()方法:判斷當前線程是否中斷,不清除中斷標志。終結(jié)狀態(tài),還是返回false。
interrupted()方法:判斷當前線程是否中斷,清除中斷標志。
如果拋出異常,中斷狀態(tài)設(shè)置為false。
示例 例子1public class InterruptThread extends Thread { @Override public void run() { while (true) { } } public static void main(String[] args) throws InterruptedException { InterruptThread thread = new InterruptThread(); thread.start(); System.out.println(thread.getState()); sleep(1000); thread.interrupt(); System.out.println(thread.getState()); System.out.println(thread.isInterrupted()); } }
運行結(jié)果如下
可以看出,雖然中斷狀態(tài)是true了,但是程序依然在運行,所以interrupt并沒有強制中斷線程。
public class InterruptThread2 extends Thread { @Override public void run() { while (!isInterrupted()) { } System.out.println("已中斷"); } public static void main(String[] args) throws InterruptedException { InterruptThread2 thread = new InterruptThread2(); thread.start(); System.out.println(thread.getState()); sleep(1000); thread.interrupt(); System.out.println(thread.getState()); System.out.println(thread.isInterrupted()); } }
運行結(jié)果如下:
跟例子1的區(qū)別是,通過判斷中斷狀態(tài),來處理我們自己的業(yè)務邏輯,這樣的設(shè)計,給程序帶來了極大的利靈活性。
public class InterruptWait extends Thread { @Override public void run() { waitFun(); } public synchronized void waitFun(){ try { wait(); } catch (InterruptedException e) { System.out.println("打擾我等待了"); } } public static void main(String[] args) throws InterruptedException { InterruptWait thread = new InterruptWait(); thread.start(); System.out.println(thread.getState()); sleep(1000); thread.interrupt(); sleep(1000); System.out.println(thread.getState()); System.out.println(thread.isInterrupted()); sleep(1000); System.out.println(thread.getState()); } }
運行結(jié)果如下:
中斷wait方法,這里需要注意的是,拋出異常后,中斷狀態(tài)變成false。
public class InterruptWait extends Thread { @Override public void run() { waitFun(); } public synchronized void waitFun(){ try { wait(); } catch (InterruptedException e) { System.out.println("打擾我等待了"); } } public static void main(String[] args) throws InterruptedException { InterruptWait thread = new InterruptWait(); thread.start(); System.out.println(thread.getState()); sleep(1000); thread.interrupt(); sleep(1000); System.out.println(thread.getState()); System.out.println(thread.isInterrupted()); sleep(1000); System.out.println(thread.getState()); } }
運行結(jié)果如下:
結(jié)果同上,拋出異常后,中斷狀態(tài)變成false。
public class InterruptSync extends Thread { @Override public void run() { syncFun(); } public static synchronized void syncFun() { while (true) { } } public static void main(String[] args) throws InterruptedException { InterruptSync thread = new InterruptSync(); InterruptSync thread2 = new InterruptSync(); thread.start(); sleep(1000); thread2.start(); sleep(1000); System.out.println(thread.getState()); System.out.println(thread2.getState()); thread2.interrupt(); sleep(1000); System.out.println(thread2.getState()); System.out.println(thread2.isInterrupted()); } }
運行結(jié)果如下:
沒有拋異常,結(jié)果同例子1。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/75248.html
摘要:不釋放持有的鎖,釋放鎖。在調(diào)用方法前,必須持有鎖,調(diào)用喚醒,也要持有鎖。休眠一定時間后,進入就緒狀態(tài)。這兩個都能被方法中斷當前狀態(tài)。用法方獲取鎖判斷條件,不滿足繼續(xù)滿足執(zhí)行其他業(yè)務方獲取鎖改變條件通知為什么是而不是會一直循環(huán),直到條件滿足。 sleep和wait sleep是Thread類的方法,wait是Object的方法。 sleep可以到處使用,wait必須是在同步方法或者代碼...
摘要:就緒狀態(tài)調(diào)用或者由阻塞狀態(tài)被解除時,進入就緒狀態(tài),此時,只能表示線程可以運行了,但不代表已經(jīng)運行了,需要等待的調(diào)度。死亡狀態(tài)當線程執(zhí)行結(jié)束或者異常等,線程就會結(jié)束,進入死亡狀態(tài)。 流程圖 showImg(https://segmentfault.com/img/bVbuJ6f); 新建狀態(tài) 當用new創(chuàng)建一個線程后,線程就處于新建狀態(tài),此時和其他普通java對象一樣,由JVM創(chuàng)建內(nèi)存空...
摘要:定義等待該線程終止,比如線程調(diào)用了線程的,那么線程要等到線程執(zhí)行完后,才可以繼續(xù)執(zhí)行。 定義 等待該線程終止,比如A線程調(diào)用了B線程的join,那么A線程要等到B線程執(zhí)行完后,才可以繼續(xù)執(zhí)行。 示例 public class JoinDemo { static class JoinThread1 implements Runnable { Thread thre...
摘要:與執(zhí)行方法,是用來啟動線程的,此時線程處于就緒狀態(tài),獲得調(diào)度后運行方法。執(zhí)行方法,相對于普通方法調(diào)用,在主線程調(diào)用。程序是順序執(zhí)行的,執(zhí)行完才會執(zhí)行下面的程序。 start與run 執(zhí)行start方法,是用來啟動線程的,此時線程處于就緒狀態(tài),獲得調(diào)度后運行run方法。run方法執(zhí)行結(jié)束,線程就結(jié)束。 執(zhí)行run方法,相對于普通方法調(diào)用,在主線程調(diào)用。程序是順序執(zhí)行的,執(zhí)行完才會執(zhí)行下...
摘要:在指定毫秒數(shù)內(nèi),讓正在執(zhí)行的當前線程進入休眠期。示例運行結(jié)果如下結(jié)果可以看出,線程的兩次時間相差毫秒,的兩次時間相差毫秒,只影響自己的線程運行,不影響其他線程。 sleep 在指定毫秒數(shù)內(nèi),讓正在執(zhí)行的當前線程進入休眠期。 示例 public class SleepDemo extends Thread { @Override public void run() { ...
閱讀 4158·2021-11-22 13:53
閱讀 1800·2021-09-23 11:52
閱讀 2574·2021-09-06 15:02
閱讀 1144·2019-08-30 15:54
閱讀 972·2019-08-30 14:15
閱讀 2459·2019-08-29 18:39
閱讀 808·2019-08-29 16:07
閱讀 603·2019-08-29 13:13