- 一个线程可用调用另一个线程的interrupt方法来试图中断另一个的运行,线程也可以中断自己,如果线程没有运行,则interrupt将不会有任何影响。当一个线程试图中断另外一个线程时,checkAccess方法会被调用来判断访问权限。之所以说是试图,是因为一个线程不能绝对中断另一个线程,interrupt只是做一个标记,需要线程自己来处理是否停止。
- 如果被中断的线程正在处于wait,join,sleep状态,那么该线程立即接受到InterruptedException异常,该异常会自动清除线程的中断状态。If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
- interrupted,测试线程是否被中断,如果中断,会自动清理中断状态
-
isInterrupted测试线程是否被中断,如果中断,不会自动清理中断状态
- 当被中断的线程没有wait,join,blocking io,selector堵塞时,中断状态不会对正在运行的线程有任何影响,需要线程主体方法代码自行处理。举例:
- 例子一,线程将不会有任何影响,会一直运行
public class App {
public static void main(String[] args) throws IOException {
Thread t = new MyThread();
t.start();
System.out.println("wait to run thread");
System.in.read();
System.out.println("wait to end thread");
t.interrupt();
System.out.println("wait to end program");
System.in.read();
}
private static class MyThread extends Thread {
long last = 0;
public void run() {
while (true) {
// if(this.isInterrupted()){
// System.out.println("thread interupted");
// return;
// }
long cur = System.currentTimeMillis();
if (last == 0) {
last = cur;
System.out.println("begin******************");
} else {
if (cur - last > 5 * 1000) {
last = cur;
System.out.println(cur);
}
}
}
}
}
}
例子二:线程自行退出
public class App {
public static void main(String[] args) throws IOException {
Thread t = new MyThread();
t.start();
System.out.println("wait to run thread");
System.in.read();
System.out.println("wait to end thread");
t.interrupt();
System.out.println("wait to end program");
System.in.read();
}
private static class MyThread extends Thread {
long last = 0;
public void run() {
while (true) {
if(this.isInterrupted()){
System.out.println("thread interupted");
return;
}
long cur = System.currentTimeMillis();
if (last == 0) {
last = cur;
System.out.println("begin******************");
} else {
if (cur - last > 5 * 1000) {
last = cur;
System.out.println(cur);
}
}
}
}
}
}
分享到:
相关推荐
Thread.interrupt()方法的使用以及使用它退出线程
interrupt code
MCS51 中断interrupt
Interrupt Example in MPLABX XC8 Compiler
interrupt,讲述飞思卡尔HCS12系列芯片的中断处理控制。
com interrupt void interrupt 4 using 3
ARM Generic Interrupt Spec, refenrence manual for arm operating sytem development
IRQ interrupt function driver
The Generic Interrupt Controller (GIC) architecture defines: • the architectural requirements for handling all interrupt sources for any processor connected to a GIC • a common interrupt controller ...
DOS 终端描述 bios interrupt , dos interrupt,interrupt services DOS,BIOS,EMS and MOUSE
微机接口课程实验interrupt.asm
电子-7interrupt.pdf,物联网/通信技术2.4G无线通信
Detection of Excessive Interrupt Disablement - a new feature which can detect a period of excessive interrupt disablement on a CPU, and create an error log record to report it. This allows you to know...
switch_counter_interrupt_TivaWare.c
Ralf Brown's Interrupt List. 6.1版 DOS中断大全,底层开发的必备工具。
程序名称:interrupt.asm 程序说明:74HC595驱动数码管动态显示例,通过按键触发外部中断,P3.2进行计数,P3.3清零,实验箱上完成 算法说明:由两片74HC595芯片分别驱动段和位,数码管动态扫描显示:修改延时子...
ARM Generic Interrupt Controller Architecture Specification 官方手册
本人利用Thread.Abort()与Thread.Interrupt()可以引起目标线程异常的特点,开发了一种不使用已过时的方法来变相从外部控制线程的挂起与恢复的技术,原理简单,方便理解。 相关技术参考:...
线程的thread.interrupt()方法是中断线程,将会设置该线程的中断状态位,即设置为true,中断的结果线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序本身。线程会不时地检测这个中断标示位,以...