一、介绍interrupt
Thread.interrupt()方法用于中断一个线程,但它的行为取决于线程当前的状态。具体来说,Thread.interrupt()方法的行为可以分为两种情况:
-
当线程处于运行状态(RUNNING)或可运行状态(RUNNABLE)时,调用interrupt()方法只是会设置线程的中断标志位,没有任何其他作用。这意味着,如果线程正在执行一个循环或其他长时间运行的任务,并且没有定期检查中断标志位,那么即使调用了interrupt()方法,线程也不会立即停止执行。为了响应中断,线程应该在运行过程中合适的位置检查中断标志位,例如,在循环的开始处进行检查。
-
当线程处于阻塞状态(如等待锁、调用sleep、wait、join等方法时),调用interrupt()方法会在线程的中断位置标记,并且通过抛出InterruptedException的方式来中断线程。这意味着,如果线程在等待资源或执行阻塞操作时被中断,它会收到一个InterruptedException,并且中断状态将被清除,这样线程就可以退出阻塞状态。
此外,Thread类提供了两个方法来检查线程的中断状态:
isInterrupted()
:这是一个实例方法,用于检查当前线程是否已经被中断。
interrupted()
:这是一个静态方法,用于检查指定线程是否已经被中断,并将中断标志位重置为false。
二、Thread.interrupt 用法
1、Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join和 Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。
2、因此,如果线程被上述几种方法阻塞,正确的停止线程方式是设置共享变量,并调用interrupt()(注意变量应该先设置)。如果线程没有被阻塞,这时调用interrupt()将不起作用;否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。在任何一种情况中,最后线程都将检查共享变量然后再停止。实例如下:
class Example3 extends Thread {
volatile boolean stop = false;
public static void main(String args[]) throws Exception {
Example3 thread = new Example3();
System.out.println("Starting thread...");
thread.start();
Thread.sleep(3000);
System.out.println("Asking thread to stop...");
thread.stop = true;//如果线程阻塞,将不会检查此变量
thread.interrupt();
Thread.sleep(3000);
System.out.println("Stopping application...");
//System.exit( 0 );
}
public void run() {
while (!stop) {
System.out.println("Thread running...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread interrupted...");
}
}
System.out.println("Thread exiting under request...");
}
}
2. 如果线程在I/O操作进行时被阻塞,Thread.interrupt()将不起作用,因为线程将不会退出被阻塞状态。
class Example5 extends Thread {
volatile boolean stop = false;
volatile ServerSocket socket;
public static void main(String args[]) throws Exception {
Example5 thread = new Example5();
System.out.println("Starting thread...");
thread.start();
Thread.sleep(3000);
System.out.println("Asking thread to stop...");
thread.stop = true;
thread.socket.close();
Thread.sleep(3000);
System.out.println("Stopping application...");
//System.exit( 0 );
}
public void run() {
try {
socket = new ServerSocket(7856);
} catch (IOException e) {
System.out.println("Could not create the socket...");
return;
}
while (!stop) {
System.out.println("Waiting for connection...");
try {
Socket sock = socket.accept();
} catch (IOException e) {
System.out.println("accept() failed or interrupted...");
}
}
System.out.println("Thread exiting under request...");
}
}
3. 一般来说,阻塞函数,如:Thread.sleep、Thread.join、Object.wait等在检查到线程的中断状态时,会抛出InterruptedException,同时会清除线程的中断状态
class MyThread extends Thread {
......
......
public void run() {
try {
while(!Thread.currentThread().isInterrupted()) { //如果线程没有被中断就继续运行
//阻塞代码:sleep,wait等
//当其他线程,调用此线程的interrupt()方法时,会给此线程设置一个中断标志
//sleep,wait等方法会检测这个标志位,同时会抛出InterruptedException,并清除线程的中断标志
//因此在异常段调用Thread.currentThread().isInterrupted()返回为false
}
} catch (InterruptedException e) {
//由于阻塞库函数,如:Object.wait,Thread.sleep除了抛出异常外,还会清除线程中断状态,因此可能在这里要保留线程的中断状态
Thread.currentThread().interrupt();//从新设置线程的中断标志
}
}
public void cancel() {
interrupt(); //中断线程
}
}
外部调用
MyThread thread = new MyThread();
thread.start(); //开启线程
......
thread.cancel(); //中断线程
System.out.println(thread.isInterrupt)//打印线程中断标志
4. 使用中断信号量中断非阻塞状态的线程,使用共享变量(shared variable)发出信号,告诉线程必须停止正在运行的任务。线程必须周期性的核查这一变量,然后有秩序地中止任务。
class Example2 extends Thread {
volatile boolean stop = false;// 线程中断信号量
public static void main(String args[]) throws Exception {
Example2 thread = new Example2();
System.out.println("Starting thread...");
thread.start();
Thread.sleep(3000);
System.out.println("Asking thread to stop...");
// 设置中断信号量
thread.stop = true;
Thread.sleep(3000);
System.out.println("Stopping application...");
}
public void run() {
// 每隔一秒检测一下中断信号量
while (!stop) {
System.out.println("Thread is running...");
long time = System.currentTimeMillis();
/*
* 使用while循环模拟 sleep 方法,这里不要使用sleep,否则在阻塞时会 抛
* InterruptedException异常而退出循环,这样while检测stop条件就不会执行,
* 失去了意义。
*/
while ((System.currentTimeMillis() - time < 1000)) {}
}
System.out.println("Thread exiting under request...");
}
}