Thread - 中断线程
线程终端方式:
1、使用stop()方法强制中断线程,该方法以做法,使用该方法可能回产生一些不可预料的结果。
2、使用退出标志,使线程正常退出,也就是让run方法正常结束。
1 package thread; 2 3 public class MyThread extends Thread{ 4 5 boolean isStop = false; 6 public void run() { 7 for (int i = 0; i < 500000; i++) { 8 if(isStop) { 9 System.out.println("线程结束"); 10 return; 11 }else { 12 System.out.println("i="+i); 13 } 14 } 15 } 16 17 public static void main(String[] args) throws InterruptedException { 18 MyThread mt = new MyThread(); 19 mt.start(); 20 Thread.sleep(2000); 21 mt.isStop = true; 22 System.out.println("end"); 23 } 24 }
执行结果:
3、使用interrupt() 方法结束线程
interrupt() 该方法不会立即中断线程,而是相当于给个标记。
如果在调用这个类的wait()、wait(long)或wait(long, int)方法或join()、join(long)、join(long)、join(long)、join(long, int)、sleep(long, int)或sleep(long, int)方法时阻塞这个线程,那么它的中断状态将被清除,并接收到一个InterruptedException异常。
1 public class MyThread2 extends Thread{ 2 3 boolean isStop = false; 4 public void run() { 5 for (int i = 0; i < 500000; i++) { 6 try { 7 Thread.sleep(50000); 8 } catch (InterruptedException e) { 9 System.out.println(this.isInterrupted()); 10 e.printStackTrace(); 11 } 12 System.out.println("i="+i); 13 } 14 } 15 16 public static void main(String[] args) throws InterruptedException { 17 MyThread2 mt = new MyThread2(); 18 mt.start(); 19 mt.interrupt(); 20 System.out.println(mt.isInterrupted()); 21 } 22 }
interrupted() 该方法是个静态方法
API描述:
测试当前线程是否被中断。该方法清除线程的中断状态。换句话说,如果连续调用这个方法两次,那么第二个调用将返回false(除非在第一个调用清除了它的中断状态并在第二个调用检查它之前,当前线程再次被中断)。
线程中断被忽略,因为在中断时线程没有活动,该方法将返回false。
1 public class MyThread2 extends Thread{ 2 3 boolean isStop = false; 4 public void run() { 5 for (int i = 0; i < 50000; i++) { 6 System.out.println("i="+i); 7 } 8 } 9 10 public static void main(String[] args) throws InterruptedException { 11 MyThread2 mt = new MyThread2(); 12 mt.start(); 13 Thread.sleep(2000); 14 mt.interrupt(); 15 System.out.println("mt.interrupted()="+mt.interrupted()); 16 System.out.println("mt.interrupted()="+mt.interrupted()); 17 } 18 }
根据上述代码可以看出,在mt线程在调interrupt()方法后,再调interrupted()方法时为啥返回false?
我们可以看下这个官方描述,interrupted()方法是测试当前线程的中断状态,这个当前线程只得是main线程,由于当前main线程没有中断所以返回false
1 public class MyThread3 { 2 3 public static void main(String[] args) throws InterruptedException { 4 Thread.currentThread().interrupt(); 5 System.out.println("Thread.interrupted()="+Thread.interrupted()); 6 System.out.println("Thread.interrupted()="+Thread.interrupted()); 7 } 8 }
可以看到当前线程中断后,再测试其中断状态时,会返回true,如果连续调用这个方法两次,那么第二个调用将返回false。
isInterrupted()
API描述:测试这个线程是否被中断。线程的中断状态不受此方法的影响。线程中断被忽略,因为在中断时线程没有活动,该方法将返回false。
1 public class MyThread2 extends Thread { 2 public void run() { 3 for (int i = 0; i < 5000000; i++) { 4 if(this.isInterrupted()) { 5 return; 6 } 7 System.out.println("i=" + i); 8 } 9 } 10 11 public static void main(String[] args) throws InterruptedException { 12 MyThread2 mt = new MyThread2(); 13 mt.start(); 14 Thread.sleep(1000); 15 mt.interrupt(); 16 System.out.println("mt.isInterrupted()=" + mt.isInterrupted()); 17 System.out.println("mt.isInterrupted()=" + mt.isInterrupted()); 18 System.out.println("end"); 19 } 20 }