<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. java語言

    java中如何停止線程

    時間:2025-03-03 22:16:26 java語言 我要投稿
    • 相關推薦

    java中如何停止線程

      Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念。想知道java中如何停止線程?下面就一起來了解看看吧!

      一般來說線程執行完run()之后就自動結束了,不過有些時候我們需要線程不停的做一些事情,也就是使用while循環,那么這時候該如何停止線程呢?

      這個問題需要分情況來討論,如果線程做的事情不是耗時的,那么只需要使用一個標志即可,具體的代碼如下:

      class MyThread extends Thread {

      private volatile boolean isStop = false;

      public void run() {

      while (!isStop) {

      System.out.println("do something");

      }

      }

      public void setStop() {

      isStop = true;

      }

      }

      如果需要退出時,調用setStop()即可。這里使用了一個Java關鍵字volatile,這個關鍵字的目的是使isStop同步,也就是說在同一時刻只能由一個線程來修改isStop的值。

      如果線程做的事情是耗時或者說阻塞的(如調用了sleep,同步鎖的wait,socket的receiver,accept等方法),那么就需要用到interrupt()了,調用該函數時會拋出InterruptedException異常,代碼中通過捕獲該異常,然后break出循環,就可以了。代碼如下:

      class MyThread extends Thread {

      private volatile boolean isStop = false;

      public void run() {

      while (!isStop) {

      try {

      System.out.println("do something");

      Thread.sleep(1000);

      } catch (InterruptedException e) {

      e.printStackTrace();

      break;//如果沒有這一句,而且不調用setStop(),線程并不會結束,需要特別注意一下

      }

      }

      }

      public void setStop() {

      isStop = true;

      }

      }

      public class Tes1 {

      public static void main(String[] args) {

      MyThread t1 = new MyThread();

      t1.start();

      try {

      Thread.sleep(10);

      t1.setStop();

      t1.interrupt();

      } catch (InterruptedException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      }

      }

      }

      這里既然說到了interrupt(),那么就不得不提另外兩個函數:interrupted()和isInterrupted。看下這兩個函數的作用:

      public static boolean interrupted()

      測試當前線程是否已經中斷,并且清除線程的中斷狀態。

      public boolean isInterrupted()

      測試線程是否已經中斷,線程的中斷狀態不受該方法的影響。

      這兩個函數的返回值含義:

      如果當前線程已經中斷,則返回 true;否則返回 false。

      其實當線程阻塞并且調用了interrupt()時,不止是拋出InterruptedException異常還會調用interrupted()來清除線程的中斷狀態,所以在catch里面調用isInterrupted()會返回false。

      為了更好的理解這兩個函數,我們看個例子:

      class MyThread extends Thread {

      private int counter = 0;

      public void run() {

      boolean done = false;

      try{

      Thread.sleep(10);

      }catch(InterruptedException ie){

      ie.printStackTrace();

      return;

      }

      while (counter < 2000 &&!done) {

      System.out.println(counter++ + " in thread isInterrupted() "+isInterrupted());

      if(isInterrupted()==true){

      try{

      System.out.println("in thread after interrupted() "+isInterrupted());

      sleep(100);

      break;

      }catch(InterruptedException ie){

      ie.printStackTrace();

      break;

      }

      }

      }

      }

      }

      public class Tes1 {

      public static void main(String[] args) {

      final MyThread t1 = new MyThread();

      t1.start();

      new Timer(true).schedule(new TimerTask() {

      public void run() {

      System.out.println("exec interrupt");

      t1.interrupt();

      System.out.println("in timer isInterrupted() "+t1.isInterrupted());

      }

      }, 20);

      }

      }

      執行效果如下(每一次執行打印的都不一樣,因為沒有做線程同步):

      ...

      1300 in thread isInterrupted() false

      exec interrupt

      in timer isInterrupted() true

      1301 in thread isInterrupted() true

      in thread after interrupted() true

      java.lang.InterruptedException: sleep interrupted

      at java.lang.Thread.sleep(Native Method)

      at com.example.wd.MyThread.run(Tes1.java:21)

      還可能是這樣:

      ...

      1568 in thread isInterrupted() false

      exec interrupt

      in thread after interrupted() true

      in timer isInterrupted() true

      java.lang.InterruptedException: sleep interrupted

      at java.lang.Thread.sleep(Native Method)

      at com.example.wd.MyThread.run(Tes1.java:21)

      由于在sleep時執行了interrupt()所以會拋出異常。

      如果把if(isInterrupted()==true)改成if(Thread.interrupted()==true)那么打印的結果就不同了,如下:

      ...

      1582 in thread isInterrupted() false

      exec interrupt

      in timer isInterrupted() true

      in thread after interrupted() false

      還可能是這樣:

      ...

      1771 in thread isInterrupted() false

      exec interrupt

      in timer isInterrupted() true

      1772 in thread isInterrupted() false

      in thread after interrupted() false

      也可能是這樣:

      ...

      476 in thread isInterrupted() false

      in thread after interrupted() false

      in timer isInterrupted() false

      雖然執行了interrupt(),但是由于Thread.interrupted()可以清除中斷,所以并不會拋出異常。

      前面我們針對線程是否耗時,給出了停止線程的方法,其實還有一個辦法那就是使用thread.stop()來強行終止線程,不過這個由于該方法不安全已經廢棄掉了,因為他有下面兩個缺陷:

      1. 立即拋出ThreadDeath異常,在線程的run()方法內,任何一點都有可能拋出ThreadDeath異常,包括在catch或finally語句中。

      2. 釋放該線程所持有的所有的鎖。


    【java中如何停止線程】相關文章:

    java如何正確停止一個線程05-25

    如何使用java多線程05-04

    如何創建并運行Java線程01-11

    JAVA中終止線程的方法08-12

    Java線程編程中的主線程詳細介紹01-31

    Java 如何進行線程同步05-19

    java Runnable接口如何創建線程05-18

    淺談如何使用java多線程05-07

    java的多線程04-09

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人