新聞中心
Java中終止線程的方式主要有三種:
為靈臺等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及靈臺網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站制作、靈臺網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
1、使用stop()方法,已被棄用。原因是:stop()是立即終止,會導(dǎo)致一些數(shù)據(jù)被到處理一部分就會被終止,而用戶并不知道哪些數(shù)據(jù)被處理,哪些沒有被處理,產(chǎn)生了不完整的“殘疾”數(shù)據(jù),不符合完整性,所以被廢棄。So, forget it!
2、使用volatile標(biāo)志位
看一個簡單的例子:
首先,實現(xiàn)一個Runnable接口,在其中定義volatile標(biāo)志位,在run()方法中使用標(biāo)志位控制程序運行
public class MyRunnable implements Runnable { //定義退出標(biāo)志,true會一直執(zhí)行,false會退出循環(huán) //使用volatile目的是保證可見性,一處修改了標(biāo)志,處處都要去主存讀取新的值,而不是使用緩存 public volatile boolean flag = true; public void run() { System.out.println("第" + Thread.currentThread().getName() + "個線程創(chuàng)建"); try { Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } //退出標(biāo)志生效位置 while (flag) { } System.out.println("第" + Thread.currentThread().getName() + "個線程終止"); } }
然后,在main()方法中創(chuàng)建線程,在合適的時候,修改標(biāo)志位,終止運行中的線程。
public class TreadTest { public static void main(String[] arg) throws InterruptedException { MyRunnable runnable = new MyRunnable(); //創(chuàng)建3個線程 for (int i = 1; i <= 3; i++) { Thread thread = new Thread(runnable, i + ""); thread.start(); } //線程休眠 Thread.sleep(2000L); System.out.println("——————————————————————————"); //修改退出標(biāo)志,使線程終止 runnable.flag = false; } }
最后,運行結(jié)果,如下:
第1個線程創(chuàng)建 第2個線程創(chuàng)建 第3個線程創(chuàng)建 -------------------------- 第3個線程終止 第1個線程終止 第2個線程終止
3、使用interrupt()中斷的方式,注意使用interrupt()方法中斷正在運行中的線程只會修改中斷狀態(tài)位,可以通過isInterrupted()判斷。如果使用interrupt()方法中斷阻塞中的線程,那么就會拋出InterruptedException異常,可以通過catch捕獲異常,然后進行處理后終止線程。有些情況,我們不能判斷線程的狀態(tài),所以使用interrupt()方法時一定要慎重考慮。
第一種:正在運行中終止
public class MyThread extends Thread { public void run(){ super.run(); try { for(int i=0; i<500000; i++){ if(this.interrupted()) { System.out.println("線程已經(jīng)終止, for循環(huán)不再執(zhí)行"); throw new InterruptedException(); } System.out.println("i="+(i+1)); } System.out.println("這是for循環(huán)外面的語句,也會被執(zhí)行"); } catch (InterruptedException e) { System.out.println("進入MyThread.java類中的catch了。。。"); e.printStackTrace(); } } }
public class Run { public static void main(String args[]){ Thread thread = new MyThread(); thread.start(); try { Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } } }
運行結(jié)果如下:
... i=203798 i=203799 i=203800 線程已經(jīng)終止, for循環(huán)不再執(zhí)行 進入MyThread.java類中的catch了。。。 java.lang.InterruptedException at thread.MyThread.run(MyThread.java:13)
第二種:阻塞狀態(tài)(sleep,wait等)終止
public class MyThread extends Thread { public void run(){ super.run(); try { System.out.println("線程開始。。。"); Thread.sleep(200000); System.out.println("線程結(jié)束。"); } catch (InterruptedException e) { System.out.println("在沉睡中被停止, 進入catch, 調(diào)用isInterrupted()方法的結(jié)果是:" + this.isInterrupted()); e.printStackTrace(); } } }
線程開始。。。 在沉睡中被停止, 進入catch, 調(diào)用isInterrupted()方法的結(jié)果是:false java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at thread.MyThread.run(MyThread.java:12)
從打印的結(jié)果來看, 如果在sleep狀態(tài)下停止某一線程,會進入catch語句,并且清除停止?fàn)顟B(tài)值,使之變?yōu)閒alse。
前一個實驗是先sleep然后再用interrupt()停止,與之相反的操作在學(xué)習(xí)過程中也要注意:
public class MyThread extends Thread { public void run(){ super.run(); try { System.out.println("線程開始。。。"); for(int i=0; i<10000; i++){ System.out.println("i=" + i); } Thread.sleep(200000); System.out.println("線程結(jié)束。"); } catch (InterruptedException e) { System.out.println("先停止,再遇到sleep,進入catch異常"); e.printStackTrace(); } } } public class Run { public static void main(String args[]){ Thread thread = new MyThread(); thread.start(); thread.interrupt(); } }
運行結(jié)果:
i=9998 i=9999 先停止,再遇到sleep,進入catch異常 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at thread.MyThread.run(MyThread.java:15)
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
當(dāng)前名稱:Java中終止線程的方法詳解
本文URL:http://www.ef60e0e.cn/article/pihhds.html