java中如何終止線程

來源:互聯網
上載者:User

標籤:

在java早期的版本中,提供了一個終止線程的方法:stop()。但在隨後的版本中,這個方法被遺棄了,因為它的中斷是”立即“,這樣有可能會造成資料不一致的情況。比如說在轉賬的過程中。因此在jdk1.5的時候,有一個方法出來解決這個問題:intertupt()。這個方法會在run()中止,【Interrupting a thread that is not alive need not have any effect.】,這樣就不會造成資料不一致的問題。但是很多人在使用這個方法的時候還是出現了問題。以下舉出一個例子:

public class ThreadTest extends Thread {   int count=0;   public void run(){       System.out.println(getName()+"將要運行...");       while(!this.isInterrupted()){           System.out.println(getName()+"運行中"+count++);           try{               Thread.sleep(400);           }catch(InterruptedException e){               System.out.println(getName()+"從阻塞中退出...");               System.out.println("this.isInterrupted()="+this.isInterrupted());           }       }       System.out.println(getName()+"已經終止!");   }}

public class ThreadDemo {        public static void main(String argv[])throws InterruptedException{        ThreadTest ta=new ThreadTest();        ta.setName("ThreadTest");        ta.start();        Thread.sleep(2000);        System.out.println(ta.getName()+"將要被中斷");        ta.interrupt();        System.out.println("ta.isInterrupted()="+ta.isInterrupted());    }}
以下給出運行結果:



可見【中斷】之後,程式還在繼續運行著。為什麼呢?sleep的時候,調用了interupt會拋出異常,讓線程重新處於非中斷狀態。修複這個bug的方法是可以用一個標誌位來解決。

public class ThreadTest extends Thread {    private boolean isInterrupted=false;     int count=0;      public void interrupt(){       isInterrupted = true;       super.interrupt();      }      public void run(){       System.out.println(getName()+"將要運行...");       while(!isInterrupted){           System.out.println(getName()+"運行中"+count++);           try{               Thread.sleep(400);           }catch(InterruptedException e){               System.out.println(getName()+"從阻塞中退出...");               System.out.println("this.isInterrupted()="+this.isInterrupted());           }       }       System.out.println(getName()+"已經終止!");   }}
這樣就可以解決以上存在問題。 

注意:

當線程再讀取大檔案資料時,調用interrupt也是無效的。這個時候,我們也可以通過標誌位,來解決。比如說中斷的狀態檢測到了,我們就強制關閉流通過調用close方法。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

java中如何終止線程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.