- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- 如何中斷LockSupport線(xiàn)程
本篇文章為大家展示了如何中斷LockSupport線(xiàn)程,內容簡(jiǎn)明扼要并且容易理解,絕對能使你眼前一亮,通過(guò)這篇文章的詳細介紹希望你能有所收獲。
如何停止、中斷一個(gè)運行中的線(xiàn)程??
線(xiàn)程api
1)面試題:如何使用中斷標識停止線(xiàn)程?
在需要中斷的線(xiàn)程中不斷監聽(tīng)中斷狀態(tài),一旦發(fā)生中斷,就執行相應的中斷處理業(yè)務(wù)邏輯。
1.修改狀態(tài)
2.停止程序的運行
2)方法
1.通過(guò)一個(gè)volatile變量實(shí)現
package com.lyy.juc;import java.util.concurrent.TimeUnit;public class InterruptDemo {private static volatile boolean isStop = false;public static void main(String[] args) {new Thread(()->{while (true){if(isStop){System.out.println(Thread.currentThread().getName()+"線(xiàn)程-----isStop = true,自己退出");break; }System.out.println("-------hello interrupt"); } },"t1").start();try {TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }isStop = true; } }
主線(xiàn)程執行,入口,發(fā)現睡了一秒,睡這一秒里,變量isStop一致為false,打印 -------hello interrupt ,一秒過(guò)去,isStop為true,線(xiàn)程t1執行t1線(xiàn)程-----isStop = true,自己退出,break出循環(huán)
2.通過(guò)通過(guò)AtomicBoolean原子類(lèi)
package com.lyy.juc;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicBoolean;public class StopThreadDemo {private static final AtomicBoolean atomicBoolean = new AtomicBoolean(true);public static void main(String[] args) {new Thread(()->{while(atomicBoolean.get()){try {//O.5s TimeUnit.MILLISECONDS.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }System.out.println("-------hello"); } }).start();try {TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }atomicBoolean.set(false); } }
3通過(guò)Thread類(lèi)自帶的中斷api方法實(shí)現
實(shí)例方法interrupt(),沒(méi)有返回值
public void interrupt()
實(shí)例方法,
調用interrupt()方法僅僅是在當前線(xiàn)程中打了一個(gè)停止的標記,并不是真正立刻停止線(xiàn)程。
實(shí)例方法isInterrupted,返回布爾值
public boolean isInterrupted()
實(shí)例方法,
獲取中斷標志位的當前值是什么,
判斷當前線(xiàn)程是否被中斷(通過(guò)檢查中斷標志位),默認是false
代碼
package com.lyy.juc;import java.util.concurrent.TimeUnit;public class InterruptDemo2{public static void main(String[] args) {Thread t1 = new Thread(() -> {while(true) {if(Thread.currentThread().isInterrupted()) {System.out.println("-----t1 線(xiàn)程被中斷了,break,程序結束");break; }System.out.println("-----hello"); } }, "t1");t1.start();System.out.println("**************"+t1.isInterrupted());//暫停1毫秒 try { TimeUnit.MILLISECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }t1.interrupt();System.out.println("**************"+t1.isInterrupted()); } }
**************false
-----hello
若干-----hello
**************true
-----t1 線(xiàn)程被中斷了,break,程序結束
4)當前線(xiàn)程的中斷標識為true,是不是就立刻停止?
具體來(lái)說(shuō),當對一個(gè)線(xiàn)程,調用 interrupt() 時(shí):
① 如果線(xiàn)程處于正?;顒?dòng)狀態(tài),那么會(huì )將該線(xiàn)程的中斷標志設置為 true,僅此而已。
被設置中斷標志的線(xiàn)程將繼續正常運行,不受影響。所以, interrupt() 并不能真正的中斷線(xiàn)程,需要被調用的線(xiàn)程自己進(jìn)行配合才行。
② 如果線(xiàn)程處于被阻塞狀態(tài)(例如處于sleep, wait, join 等狀態(tài)),在別的線(xiàn)程中調用當前線(xiàn)程對象的interrupt方法,
那么線(xiàn)程將立即退出被阻塞狀態(tài),并拋出一個(gè)InterruptedException異常。
package com.lyy.juc;import java.util.concurrent.TimeUnit;public class InterruptDemo3 {public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread(() -> {for (int i = 0; i < 3; i++) {System.out.println("-------" + i); }System.out.println("after t1.interrupt()--第2次---: " + Thread.currentThread().isInterrupted()); }, "t1");t1.start();System.out.println("before t1.interrupt()----: " + t1.isInterrupted());//實(shí)例方法interrupt()僅僅是設置線(xiàn)程的中斷狀態(tài)位設置為true,不會(huì )停止線(xiàn)程 t1.interrupt();//活動(dòng)狀態(tài),t1線(xiàn)程還在執行中 try {TimeUnit.MILLISECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }System.out.println("after t1.interrupt()--第1次---: " + t1.isInterrupted());//非活動(dòng)狀態(tài),t1線(xiàn)程不在執行中,已經(jīng)結束執行了。 try {TimeUnit.MILLISECONDS.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }System.out.println("after t1.interrupt()--第3次---: " + t1.isInterrupted()); } }
循環(huán)次數不同,結果順序不一樣,不過(guò)最終狀態(tài)改完后三個(gè)都為true
中斷只是一種協(xié)同機制,修改中斷標識位僅此而已,不是立刻stop打斷
6)靜態(tài)方法Thread.interrupted()
7)LockSupport
LockSupport是用來(lái)創(chuàng )建鎖和其他同步類(lèi)的基本線(xiàn)程阻塞原語(yǔ)。
下面這句話(huà),后面詳細說(shuō)
LockSupport中的park() 和 unpark() 的作用分別是阻塞線(xiàn)程和解除阻塞線(xiàn)程
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站