Javaでスレッドを終了する方法の詳細

5043 ワード

Javaでスレッドを終了する方法は主に3つあります.
1、stop()メソッドを使用して、廃棄されました.なぜならstop()は直ちに終了し、一部のデータが処理されると終了するが、ユーザーはどのデータが処理されているのか、処理されていないのか、不完全な「障害」データが発生し、完全性に合致しないため廃棄されるからである.So, forget it!
2、volatileフラグビットの使用
簡単な例を見てみましょう
まず,volatileフラグビットを定義するRunnableインタフェースを実現し,run()メソッドでフラグビット制御プログラムを用いて実行する

public class MyRunnable implements Runnable { 

 //      ,true     ,false      
 //  volatile        ,       ,            ,        
 public volatile boolean flag = true; 

 public void run() { 
  System.out.println(" " + Thread.currentThread().getName() + "     "); 

  try { 
   Thread.sleep(1000L); 
  } catch (InterruptedException e) { 
   e.printStackTrace(); 
  } 

  //         
  while (flag) { 
  } 
  System.out.println(" " + Thread.currentThread().getName() + "     "); 
 } 
}


次にmain()メソッドでスレッドを作成し、適切なときにフラグビットを変更し、実行中のスレッドを終了します.

public class TreadTest { 
 public static void main(String[] arg) throws InterruptedException { 
  MyRunnable runnable = new MyRunnable(); 

  //  3    
  for (int i = 1; i <= 3; i++) { 
   Thread thread = new Thread(runnable, i + ""); 
   thread.start(); 
  } 
  //     
  Thread.sleep(2000L); 
  System.out.println("――――――――――――――――――――――――――"); 
  //      ,      
  runnable.flag = false; 
 } 
}


最後に、実行結果は次のとおりです.

 1     
 2     
 3     
--------------------------
 3     
 1     
 2     

3、interrupt()割り込みを使用する方法、interrupt()メソッドを使用して実行中のスレッドを割り込むと割り込みステータスビットが変更されるだけで、isInterrupted()で判断できます.interrupt()メソッドを使用してブロック内のスレッドを中断すると、InterruptedException例外が放出され、catchで例外をキャプチャして処理してスレッドを終了できます.スレッドの状態を判断できない場合があるので,interrupt()メソッドを使用する場合は慎重に考慮しなければならない.
1つ目:実行中に終了

public class MyThread extends Thread {
 public void run(){
  super.run();
  try {
   for(int i=0; i<500000; i++){
    if(this.interrupted()) {
     System.out.println("      , for      ");
      throw new InterruptedException();
    }
    System.out.println("i="+(i+1));
   }

   System.out.println("  for       ,     ");
  } 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();
  }
 }
}

実行結果は次のとおりです.

...
i=203798
i=203799
i=203800
      , for      
  MyThread.java   catch 。。。
java.lang.InterruptedException
 at thread.MyThread.run(MyThread.java:13)

2つ目:ブロック状態(sleep,waitなど)終了

public class MyThread extends Thread {
 public void run(){
  super.run();

  try {
   System.out.println("    。。。");
   Thread.sleep(200000);
   System.out.println("    。");
  } catch (InterruptedException e) {
   System.out.println("       ,   catch,   isInterrupted()      :" + this.isInterrupted());
   e.printStackTrace();
  }

 }
}


    。。。
       ,   catch,   isInterrupted()      :false
java.lang.InterruptedException: sleep interrupted
 at java.lang.Thread.sleep(Native Method)
 at thread.MyThread.run(MyThread.java:12)

印刷の結果、sleep状態でスレッドを停止するとcatch文に入り、停止状態値をfalseに変更します.
前の実験はsleepを先にしてinterrupt()で停止し、それとは逆の操作は学習中にも注意しなければならない.

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("    。");
  } 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();
 }
}


実行結果:

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)

読書に感謝して、みんなを助けることができることを望んで、みんなの当駅に対する支持に感謝します!