Java Thread.interrupt 用法
1. Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join和 Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。因此,如果线程被上述几种方法阻塞,正确的停止线程方式是设置共享变量,并调用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..."); } }
一旦Thread.interrupt()被调用,线程便收到一个异常,于是逃离了阻塞状态并确定应该停止。运行以上代码将得到下面的输出:
Starting thread... Thread running... Thread running... Thread running... Asking thread to stop... Thread interrupted... Thread exiting under request... Stopping application...
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..."); } }
运行代码后的输出:
Starting thread... Waiting for connection... Asking thread to stop... accept() failed or interrupted... Thread exiting under request... Stopping application...
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..."); } }
版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。