JavaSE8基礎 多線程interrupt 線程中斷
阿新 • • 發佈:2017-10-09
dex try over 中斷 package 2-2 lee 思考 method
禮悟:
好好學習多思考,尊師重道存感恩。葉見尋根三返一,活水清源藏於零。
虛懷若谷良心主,皓月當空自在王。願給最苦行無悔,誠勸且行且珍惜。
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 { @Override public void run() { // TODO Auto-generated method stub super.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 block e.printStackTrace(); } } }
結果:
API:
Java優秀,值得學習。
學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。
JavaSE8基礎 多線程interrupt 線程中斷