线程中断
//中断主线程
Thread.currentThread().interrupt();//线程自己中断自己
Thread worker = new Thread();
worker.start();
worker.interrupt();//线程A中断线程B
Thread.interrupted();//检测中断状态,并清除中断状态,中断标记置为false
worker.isInterrupted();//检测中断状态,不会改变中断状态
package org.thread;
public class InterruptDemo1 {
public static void main(String[] args) {
//中断主线程
Thread.currentThread().interrupt();
Worker worker = new Worker();
Thread t = new Thread(worker);
t.start();
System.out.println("Main Thread.isInterrupted: "+Thread.currentThread().isInterrupted());
try {
Thread.sleep(100);//由于主线程处于中断状态,调用sleep()将会抛出InterruptedException
} catch (InterruptedException e) {
/**
* 在catch块中捕获到被抛出的Interrupted异常时,被中断线程的中断状态已经被清空(中断状态=false),以便确保该线程处于正常状态继续执行
* 因为中断仅仅是立即停止线程当前正在做的事,在catch到中断之后,可以让该线程去做另一件事情,所以进入catch块时中断状态实际上已经置为false了!
* 至于如何处理中断异常,需要根据具体情况而定:
* 1. return 退出循环/方法
* 2. 抛出InterruptedException到上层
* 3. 返回到线程池中,继续准备为下一个请求进行服务
*/
System.out.println("The interrupted status of the current thread is cleared when this exception is thrown.");
System.out.println("Main Thread.isInterrupted: "+Thread.currentThread().isInterrupted());//false
//Clear Interrupted status, just make sure. Don't confused!
Thread.interrupted();
System.out.println("Main Thread.isInterrupted: "+Thread.currentThread().isInterrupted());//false
}
t.interrupt();
}
private static class Worker implements Runnable {
@Override
public void run() {
//doWork01();
doWork02();
}
/**
* 此循环不会因为别的线程调用了本线程的interrupt()而停止
* 因为,既没有中断异常的捕获,也没有中断状态的检测,所以,程序不会对中断做出任何响应
*/
private void doWork01() {
while(true)
System.out.println("Interrupte Has No Effect To Me!");
}
/**
* 在循环中主动检测中断状态,当发现自己被中断时,退出
*/
private void doWork02() {
while(true) {
System.out.println("Because there is no interruptedException can be thrown, so we need to check 'interrupt status' ourselves!");
if(Thread.currentThread().isInterrupted()) {//or Thread.interrupted()
System.out.println("We have been interrupted, so just stop the work and return to home!");
return;
}
}
}
}
}
超时后中断线程:
package org.thread;
/**
* 监视线程,超时便中断
*/
public class InterruptDemo2 {
public static void main(String[] args) throws InterruptedException {
Worker worker = new Worker();
Thread t = worker.work();
int patience = 5000;//5s
long start = System.currentTimeMillis();
while(t.isAlive()) {
printMessage("Still waiting...");
t.join(1000);//主线程等待1s后再执行
if(System.currentTimeMillis()-start > patience && t.isAlive()) {
printMessage("Tired of waiting!!!");
t.interrupt();
t.join();//主线程无限等待,直到t线程结束后,主线程才继续往下执行
}
}
System.out.println("Over!");
}
private static void printMessage(String message) {
String tName = Thread.currentThread().getName();
System.out.format("%s: %s%n", tName, message);
}
private static class Worker implements Runnable {
final String[] messages = {"AAAAA","BBBBB","CCCCC","DDDD"};
@Override
public void run() {
for(int index=0; index<messages.length; index++) {
try {
Thread.sleep(2000);
printMessage(messages[index]);
} catch (InterruptedException e) {
printMessage("Not finished");
return;
}
}
}
public Thread work() {
Thread t = new Thread(this);
t.start();
return t;
}
}
}