Java interrupt 中断
作者:互联网
为什么要中断?
根据需要停止某些持续的方法,这些方法可以被中断,所以又被称为可中断方法,包括:
Object的wait(), wait(long), wait(long, int),
Thraed的sleep(long), sleep(long, int), join(), join(long), join(long, int),
InterruptibleChannel的io操作,
Selector的wakeup方法,
其他方法。
如何中断?
Thread的三个方法:
方法 | 功能 | 特点 |
public void interrupt() | 中断当前线程 | 更新中断标记为true,抛出InterruptedException |
public void static boolean interrupted() | 检查线程中断标记 | 返回查中断标记 |
public boolean isInterrupted() | 检查线程中断标记,可能更新 | 返回中断标记,若为true则更新为false。 |
当线程被中断时,会将中断标记置为true,并抛出InterruptedException异常,当捕获到这个异常时,说明当前线程的中断方法被调用。
如果在可中断方法执行前,调用了interrupt()方法,那么当执行到可中断方法时,会立刻抛出InterruptedException。
线程中catch (InterruptedException ex)会导致中断标志被擦除。
示例
interrupt:
public static void main(String[] args) throws InterruptedException { Thread thread = new Thread() { @Override public void run() { while (true) { try { TimeUnit.MINUTES.sleep(2); } catch (InterruptedException ex) { System.out.println("I am interrupted"); } } } }; thread.start(); TimeUnit.MICROSECONDS.sleep(2); thread.interrupt(); }
output:
I am interrupted
interrupted:
public static void main(String[] args) throws InterruptedException { Thread thread = new Thread() { @Override public void run() { while (true) { //移除sleep和try-catch,因为可中断方法中断时,会将中断标志置为false } } }; thread.start(); TimeUnit.MICROSECONDS.sleep(2); System.out.println("before invoke interrupte,thread is interrupted:" + thread.isInterrupted()); thread.interrupt(); System.out.println("after invoke interrupte is interrupted:" + thread.isInterrupted());
}
output:
before invoke interrupte,thread is interrupted:false
after invoke interrupte is interrupted:true
isInterrupted
public static void main(String[] args) throws InterruptedException { Thread thread = new Thread() { @Override public void run() { while (true) { System.out.println("thread is interrupted:" + Thread.interrupted()); } } }; thread.setDaemon(true); thread.start(); TimeUnit.MICROSECONDS.sleep(2); System.out.println("before invoke interrupte,thread is interrupted:" + thread.isInterrupted()); thread.interrupt(); System.out.println("after invoke interrupte is interrupted:" + thread.isInterrupted()); TimeUnit.MICROSECONDS.sleep(1); System.out.println("finally thread is interrupted:" + thread.isInterrupted()); }
输出:
output:
thread is interrupted:false
before invoke interrupte,thread is interrupted:false
thread is interrupted:false
after invoke interrupte is interrupted:true
thread is interrupted:true
thread is interrupted:false
finally thread is interrupted:false
可中断方法执行前调用interrupt
public static void main(String[] args) { System.out.println("thread is interrupted:" + Thread.interrupted()); Thread.currentThread().interrupt(); System.out.println("after interrupt,do something..."); try { TimeUnit.MICROSECONDS.sleep(2); } catch (InterruptedException e) { System.out.println("thread is interrupted:" + Thread.currentThread().isInterrupted()); } }
输出:
thread is interrupted:false after interrupt,do something... thread is interrupted:false
标签:Java,Thread,thread,中断,interrupted,println,interrupt 来源: https://www.cnblogs.com/lvjianwei/p/10577495.html