中断线程详解(Interrupt)
在上篇文章《多线程的使用——Thread类和Runnable接口》中提到中断线程的问题。在JAVA中,曾经使用stop方法来停止线程,然而,该方法具有固有的不安全性,因而已经被抛弃(Deprecated)。那么应该怎么结束一个进程呢?官方文档中对此有详细说明:《为何不赞成使用 Thread.stop、Thread.suspend 和 Thread.resume?》。在此引用stop方法的说明:
- private Thread blinker;
- public void start() {
- blinker = new Thread(this);
- blinker.start();
- }
- public void stop() {
- blinker.stop(); // UNSAFE!
- }
- public void run() {
- Thread thisThread = Thread.currentThread();
- while (true) {
- try {
- thisThread.sleep(interval);
- } catch (InterruptedException e){
- }
- repaint();
- }
- }
- private volatile Thread blinker;
- public void stop() {
- blinker = null;
- }
- public void run() {
- Thread thisThread = Thread.currentThread();
- while (blinker == thisThread) {
- try {
- thisThread.sleep(interval);
- } catch (InterruptedException e){
- }
- repaint();
- }
- }
- package com.polaris.thread;
- public class TestThread implements Runnable{
- boolean stop = false;
- public static void main(String[] args) throws Exception {
- Thread thread = new Thread(new TestThread(),"My Thread");
- System.out.println( "Starting thread..." );
- thread.start();
- Thread.sleep( 3000 );
- System.out.println( "Interrupting thread..." );
- thread.interrupt();
- System.out.println("线程是否中断:" + thread.isInterrupted());
- Thread.sleep( 3000 );
- System.out.println("Stopping application..." );
- }
- public void run() {
- while(!stop){
- System.out.println( "My Thread is running..." );
- // 让该循环持续一段时间,使上面的话打印次数少点
- long time = System.currentTimeMillis();
- while((System.currentTimeMillis()-time < 1000)) {
- }
- }
- System.out.println("My Thread exiting under request..." );
- }
- }
- package com.polaris.thread;
- public class TestThread2 implements Runnable{
- boolean stop = false;
- public static void main(String[] args) throws Exception {
- Thread thread = new Thread(new TestThread2(),"My Thread2");
- System.out.println( "Starting thread..." );
- thread.start();
- Thread.sleep( 3000 );
- System.out.println( "Interrupting thread..." );
- thread.interrupt();
- System.out.println("线程是否中断:" + thread.isInterrupted());
- Thread.sleep( 3000 );
- System.out.println("Stopping application..." );
- }
- public void run() {
- while(!stop){
- System.out.println( "My Thread is running..." );
- // 让该循环持续一段时间,使上面的话打印次数少点
- long time = System.currentTimeMillis();
- while((System.currentTimeMillis()-time < 1000)) {
- }
- if(Thread.currentThread().isInterrupted()) {
- return;
- }
- }
- System.out.println("My Thread exiting under request..." );
- }
- }
注意:interrupted与isInterrupted方法的区别(见API文档)
引用一篇文章
来自随心所欲http://redisliu.blog.sohu.com/131647795.html 的《Java的interrupt机制》
当外部线程对某线程调用了thread.interrupt()方法后,java语言的处理机制如下:
如果该线程处在可中断状态下,(调用了xx.wait(),或者Selector.select(),Thread.sleep()等特定会发生阻塞的api),那么该线程会立即被唤醒,同时会受到一个InterruptedException,同时,如果是阻塞在io上,对应的资源会被关闭。如果该线程接下来不执行“Thread.interrupted()方法(不是interrupt),那么该线程处理任何io资源的时候,都会导致这些资源关闭。当然,解决的办法就是调用一下interrupted(),不过这里需要程序员自行根据代码的逻辑来设定,根据自己的需求确认是否可以直接忽略该中断,还是应该马上退出。
如果该线程处在不可中断状态下,就是没有调用上述api,那么java只是设置一下该线程的interrupt状态,其他事情都不会发生,如果该线程之后会调用行数阻塞API,那到时候线程会马会上跳出,并抛出InterruptedException,接下来的事情就跟第一种状况一致了。如果不会调用阻塞API,那么这个线程就会一直执行下去。除非你就是要实现这样的线程,一般高性能的代码中肯定会有wait(),yield()之类出让cpu的函数,不会发生后者的情况。