JavaSE8基礎 多線程interrupt 線程中斷

來源:互聯網
上載者:User

標籤:lease   ace   pac   int   clip   技術   思考   測試   trace   

禮悟:
           好好學習多思考,尊師重道存感恩。葉見尋根三返一,活水清源藏於零。
           虛懷若穀良心主,皓月當空自在王。願給最苦行無悔,誠勸且行且珍惜。



    os :windows7 x64
    jdk:jdk-8u131-windows-x64
    ide:Eclipse Oxygen Release (4.7.0)



Runnable介面實作類別:

package blog.jizuiku4;/* * @version V17.09 */public class MyThread extends Thread {@Overridepublic void run() {// TODO Auto-generated method stubsuper.run();System.out.println("線程開始");try {Thread.sleep(1000*5);} catch (InterruptedException e) {// TODO Auto-generated catch block// e.printStackTrace();System.out.println("InterruptedException異常");}System.out.println("線程結束");}}

 
測試類別:

package blog.jizuiku4;/* * @author jizuiku  * @version V17.09.27 */public class InterruptDemo {public static void main(String[] args) {MyThread mt = new MyThread();mt.start();try {Thread.sleep(3000);
              // 強行中斷線程,會引起觸發 線程類中的異常if (!mt.isInterrupted()) {mt.interrupt();}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

 
結果:    

 

API:

 

 

Java優秀,值得學習。
學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。

JavaSE8基礎 多線程interrupt 線程中斷

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.