public void Thread.interrupt() // 无返回值
public boolean Thread.isInterrupted() // 有返回值
public static boolean Thread.interrupted() // 静态,有返回值
使用interrupt()中断线程
当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一个标志,表示它已经被中断,并立即返回。这里需要注意的是,如果只是单纯的调用interrupt()方法,线程并没有实际被中断,会继续往下执行。
public class InterruptTest { public static void main(String[] args) throws InterruptedException { MyThread t = new MyThread("MyThread"); t.start(); Thread.sleep(100);// 睡眠100毫秒 t.interrupt();// 中断t线程 } } class MyThread extends Thread { int i = 0; public MyThread(String name) { super(name); } public void run() { while(true) {// 死循环,等待被中断 System.out.println(getName() + getId() + "执行了" + ++i + "次"); } } }
运行后,我们发现,线程t一直在执行,没有被中断,原来interrupt()是骗人的,汗!其实interrupt()方法并不是中断线程的执行,而是为调用该方法的线程对象打上一个标记,设置其中断状态为true,通过isInterrupted()方法可以得到这个线程状态,我们将上面的程序做一个小改动:
public class InterruptTest { public static void main(String[] args) throws InterruptedException { MyThread t = new MyThread("MyThread"); t.start(); Thread.sleep(100);// 睡眠100毫秒 t.interrupt();// 中断t线程 } } class MyThread extends Thread { int i = 0; public MyThread(String name) { super(name); } public void run() { while(!isInterrupted()) {// 当前线程没有被中断,则执行 System.out.println(getName() + getId() + "执行了" + ++i + "次"); } } }
这样的话,线程被顺利的中断执行了。很多人实现一个线程类时,都会再加一个flag标记,以便控制线程停止执行,其实完全没必要,通过线程自身的中断状态,就可以完美实现该功能。如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。 我们可以捕获该异常,并且做一些处理。另外,Thread.interrupted()方法是一个静态方法,它是判断当前线程的中断状态,需要注意的是,线程的中断状态会由该方法清除。换句话说,如果连续两次调用该方法,则第二次调用将返回 false(在第一次调用已清除了其中断状态之后,且第二次调用检验完中断状态前,当前线程再次中断的情况除外)。
下面一段代码演示了休眠线程的中断:
public class SleepInterrupt extends Object implements Runnable{ public void run(){ try{ System.out.println("in run() - about to sleep for 20 seconds"); Thread.sleep(20000); System.out.println("in run() - woke up"); }catch(InterruptedException e){ System.out.println("in run() - interrupted while sleeping"); //处理完中断异常后,返回到run()方法人口, //如果没有return,线程不会实际被中断,它会继续打印下面的信息 return; } System.out.println("in run() - leaving normally"); } public static void main(String[] args) { SleepInterrupt si = new SleepInterrupt(); Thread t = new Thread(si); t.start(); //主线程休眠2秒,从而确保刚才启动的线程有机会执行一段时间 try { Thread.sleep(2000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println("in main() - interrupting other thread"); //中断线程t t.interrupt(); System.out.println("in main() - leaving"); } }
运行结果如下:
待决中断
在上面的例子中,sleep()方法的实现检查到休眠线程被中断,它会相当友好地终止线程,并抛出InterruptedException异常。另外一种情况,如果线程在调用sleep()方法前被中断,那么该中断称为待决中断,它会在刚调用sleep()方法时,立即抛出InterruptedException异常。
下面的代码演示了待决中断:
public class PendingInterrupt extends Object { public static void main(String[] args){ //如果输入了参数,则在mian线程中中断当前线程(亦即main线程) if( args.length > 0 ){ Thread.currentThread().interrupt(); } //获取当前时间 long startTime = System.currentTimeMillis(); try{ Thread.sleep(2000); System.out.println("was NOT interrupted"); }catch(InterruptedException x){ System.out.println("was interrupted"); } //计算中间代码执行的时间 System.out.println("elapsedTime=" + ( System.currentTimeMillis() - startTime)); } }
使用isInterrupted()方法判断中断状态
使用Thread.interrupted()方法判断中断状态
可以使用Thread.interrupted()方法来检查当前线程的中断状态(并隐式重置为false)。又由于它是静态方法,因此不能在特定的线程上使用,而只能报告调用它的线程的中断状态,如果线程被中断,而且中断状态尚不清楚,那么,这个方法返回true。与isInterrupted()不同,它将自动重置中断状态为false,第二次调用Thread.interrupted()方法,总是返回false,除非中断了线程。
如下代码演示了Thread.interrupted()方法的使用:
public class InterruptReset extends Object { public static void main(String[] args) { System.out.println( "Point X: Thread.interrupted()=" + Thread.interrupted()); Thread.currentThread().interrupt(); System.out.println( "Point Y: Thread.interrupted()=" + Thread.interrupted()); System.out.println( "Point Z: Thread.interrupted()=" + Thread.interrupted()); } }