<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                在jdk中關于interrupt相關方法有三個: ![](https://img.kancloud.cn/d1/fb/d1fba3536dd5624e911c11afd16cf002_1070x278.png) 簡單來使用一下interrupt: ```java /** * @program: ThreadDemo * @description: 線程中斷 * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadInterrupt { public static void main(String[] args) { Thread t1 = new Thread("t1") { @Override public void run() { while (true) { } } }; t1.start(); // start之后處于runnable,并不一定馬上就會running。所以設置短暫休眠等待t1啟動 try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(t1.isInterrupted()); t1.interrupt(); System.out.println(t1.isInterrupted()); } } ``` 運行效果如下: ![](https://img.kancloud.cn/fa/51/fa51217eb61badf7eeb09ec2c6ee5f53_960x265.gif) 可以看到:沒有中斷的線程中斷了,但是程序卻沒有結束。interrupt()不能中斷在運行中的線程,它只能改變中斷狀態而已。 找到interrupt()方法的文檔: ![](https://img.kancloud.cn/13/b8/13b820e5063d27008deafae3a7db31b2_1040x342.png) 可以看到如果線程中調用wait,join,sleep會捕獲InterruptedException: 我們先用sleep()方法試一下,代碼如下: ```java /** * @program: ThreadDemo * @description: 線程中斷 * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadInterrupt { public static void main(String[] args) { Thread t1 = new Thread("t1") { @Override public void run() { while (true) { try { Thread.sleep(3_1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }; t1.start(); // start之后處于runnable,并不一定馬上就會running。所以設置短暫休眠等待t1啟動 try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(t1.isInterrupted()); t1.interrupt(); System.out.println(t1.isInterrupted()); } } ``` ![](https://img.kancloud.cn/08/75/087549e898bd775555e27b832fed461f_960x265.gif) 再試一下wait()方法: ```java /** * @program: ThreadDemo * @description: 線程中斷 * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadInterrupt { private static final Object MONITOR = new Object(); public static void main(String[] args) { Thread t1 = new Thread("t1") { @Override public void run() { while (true) { synchronized (MONITOR) { try { MONITOR.wait(1_000); } catch (InterruptedException e) { e.printStackTrace(); } } } } }; t1.start(); // start之后處于runnable,并不一定馬上就會running。所以設置短暫休眠等待t1啟動 try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(t1.isInterrupted()); t1.interrupt(); System.out.println(t1.isInterrupted()); } } ``` 運行效果如下: ![](https://img.kancloud.cn/16/96/1696ab8c986bd3711ab0e3c9fb3da3bd_960x265.gif) 再試一下join(): ```java /** * @program: ThreadDemo * @description: join中斷線程 * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadInterrupt { private static final Object MONITOR = new Object(); public static void main(String[] args) { Thread t1 = new Thread(() -> { while (true) { } }, "t1"); t1.start(); Thread t2 = new Thread(() -> { try { Thread.sleep(100); t1.interrupt(); System.out.println("interrupt t1 thread"); } catch (InterruptedException e) { e.printStackTrace(); } }); t2.start(); try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } ``` 運行效果如下: ![](https://img.kancloud.cn/4a/5a/4a5ae3ee8d2a4f37b0724a07313eab69_960x205.gif) 線程t1被中斷了,但是為什么沒收到中斷異常呢? 其實join()的是main線程,而我們打斷的是t1線程,主線程的join()不會收到中斷異常,所以代碼改造如下: ```java /** * @program: ThreadDemo * @description: join中斷線程 * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadInterrupt { private static final Object MONITOR = new Object(); public static void main(String[] args) { Thread t1 = new Thread(() -> { while (true) { } }, "t1"); t1.start(); Thread main = Thread.currentThread(); Thread t2 = new Thread(() -> { try { Thread.sleep(100); main.interrupt(); System.out.println("interrupt t1 thread"); } catch (InterruptedException e) { e.printStackTrace(); } }); t2.start(); try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } ``` 運行效果如下: ![](https://img.kancloud.cn/84/5e/845e863c93eda987efc474213c3cc487_960x205.gif) 同樣也捕獲到了中斷異常,但都沒有讓t線程退出,我們來看一下join的源碼: ![](https://img.kancloud.cn/0b/15/0b1508537a609fd344a60be7da282b4d_652x445.png) 其實join里也是用到了wait()方法的。 那么怎么能讓t1線程退出呢,這里有個簡單粗暴的方法,但是官方并不推薦,就是stop()方法: 我們在代碼的結尾加上 ``` t1.stop(); ``` 運行效果如下: ![](https://img.kancloud.cn/c3/2c/c32c3a5915cd8c6c439bdb2ee2b0fb5e_960x265.gif) 已經實現了我們想要的效果,但是并不推薦這么用,那么到底該怎么優雅的中斷線程呢? ## 用一個標記來控制 ```java /** * @program: ThreadDemo * @description: 優雅地停止線程 Graceful thread stop 1. 使用開關變量控制是否啟動 * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadCloseGraceful { private static class Worker extends Thread { private volatile boolean start = true; @Override public void run() { while (start) { // ... } } public void shutdown() { this.start = false; } } public static void main(String[] args) throws InterruptedException { Worker worker = new Worker(); worker.start(); Thread.sleep(2_000); worker.shutdown(); } } ``` 運行效果如下: ![](https://img.kancloud.cn/1c/c0/1cc05f651bd17171be39191ed57c5d80_960x205.gif) ## 利用打斷機制 ```java /** * @program: ThreadDemo * @description: 優雅地停止線程 Graceful thread stop 通過打斷的方式實現 * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadCloseGraceful2 { private static class Worker extends Thread { @Override public void run() { while (true) { // 1. sleep() /*try { Thread.sleep(1); } catch (InterruptedException e) { break;// return; }*/ // 2. if if (Thread.interrupted()) { break;// return; } } // ... catch中使用break;可以在while()后執行其他操作 System.out.println("break-opera after while"); } } public static void main(String[] args) throws InterruptedException { Worker worker = new Worker(); worker.start(); Thread.sleep(3_000); worker.interrupt(); } } ``` 運行效果如下: ![](https://img.kancloud.cn/f0/c2/f0c2d2d4138d6f0041087e46edd3ca98_960x205.gif) 上面兩個方法確實可以中斷線程,但是有這樣一個問題改如何解決呢? ![](https://img.kancloud.cn/b8/60/b860491bfc144760a77473a934987798_685x258.png) 也就是除l了第一次判斷了一下interrupted,后面就被阻塞了,那么改如何解決呢?這個我們后續再學習。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看