Java Thread Stop方法以及替換實現

來源:互聯網
上載者:User

 

 

Stop方法不推薦使用,我給個具體的例子:

  1. public class DeprecatedStop extends Object implements Runnable {
  2.     public void run() {
  3.         int count = 0;
  4.         while ( count <20 ) {
  5.             System.out.println("Running ... count=" + count);
  6.             count++;
  7.             try {
  8.                 Thread.sleep(300);
  9.             } catch ( InterruptedException x ) {
  10.                 // ignores
  11.             }
  12.         }
  13.         
  14.         // the code maybe not executed 
  15.         System.out.println("stoped");
  16.     }
  17.     public static void main(String[] args) {
  18.         DeprecatedStop ds = new DeprecatedStop();
  19.         Thread t = new Thread(ds);
  20.         t.start();
  21.         try {
  22.             Thread.sleep(2000);
  23.         } catch ( InterruptedException x ) {
  24.             // ignore
  25.         }
  26.         // Abruptly stop the other thread in its tracks!
  27.         t.stop();
  28.     }
  29. }

 

 

可能的運行結果:

  1. Running ... count=0
  2. Running ... count=1
  3. Running ... count=2
  4. Running ... count=3
  5. Running ... count=4
  6. Running ... count=5
  7. Running ... count=6

可以發現程式中的列印stoped並沒有執行,所以說如果在程式中有其他動作,如果線程突然stop是會帶來嚴重的影響的。所以怎麼也應該使用該操作。當然如果是我上面的程式碼突然stop的影響其實是沒有的,但是如果是其他開啟檔案最後需要釋放或者什麼的就會帶來嚴重的影響了。

 

如何在程式中對其進行停止呢?

 

  1. public class AlternateStop extends Object implements Runnable {
  2.     private volatile boolean stopRequested;
  3.     private Thread runThread;
  4.     public void run() {
  5.         runThread = Thread.currentThread();
  6.         stopRequested = false;
  7.         int count = 0;
  8.         while ( !stopRequested ) {
  9.             System.out.println("Running ... count=" + count);
  10.             count++;
  11.             try {
  12.                 Thread.sleep(300);
  13.             } catch ( InterruptedException x ) {
  14.                 Thread.currentThread().interrupt(); // re-assert interrupt
  15.             }
  16.         }
  17.         
  18.         System.out.println("stoped");
  19.     }
  20.     public void stopRequest() {
  21.         stopRequested = true;
  22.         if ( runThread != null ) {
  23.             runThread.interrupt();
  24.         }
  25.     }
  26.     public static void main(String[] args) {
  27.         AlternateStop as = new AlternateStop();
  28.         Thread t = new Thread(as);
  29.         t.start();
  30.         try {
  31.             Thread.sleep(2000);
  32.         } catch ( InterruptedException x ) {
  33.             // ignore
  34.         }
  35.         as.stopRequest();
  36.     }
  37. }

可能的運行結果如下:

  1. Running ... count=0
  2. Running ... count=1
  3. Running ... count=2
  4. Running ... count=3
  5. Running ... count=4
  6. Running ... count=5
  7. Running ... count=6
  8. stoped

這樣我們就解決了強制 stop的問題。

 

 

聯繫我們

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