java interrupt()方法

Thread.interrupt并不会中断一个正在运行的线程

package thread;

class Test extends Thread {
    boolean stop=false;
    public static void main( String args[] ) throws Exception {
        Test thread = new Test();
        System.out.println( "Starting thread..." );
        thread.start();
        Thread.sleep( 3000 );
        System.out.println( "Interrupting thread..." );
        thread.interrupt();
        Thread.sleep( 3000 );
        System.out.println("Stopping application..." );
        //System.exit(0);
    }
    public void run() {
        while(!stop){
            System.out.println( "Thread is running..." );
            long time = System.currentTimeMillis();
            while((System.currentTimeMillis()-time < 1000)) {
            }
        }
        System.out.println("Thread exiting under request..." );
    }
}
控制台运行结果如下:

Starting thread...
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Interrupting thread...
Thread is running...
Thread is running...

....

在Thread.interrupt()被调用后,线程仍然继续运行。


使用Thread.interrupt()中断线程 

Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。 

因此,如果线程被上述几种方法阻塞,正确的停止线程方式是设置共享变量,并调用interrupt()(注意变量应该先设置)。如果线程没有被阻塞,这时调用interrupt()将不起作用;否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。在任何一种情况中,最后线程都将检查共享变量然后再停止。

package thread;

class Test extends Thread {
    volatile boolean stop = false;
    public static void main( String args[] ) throws Exception {
        Test thread = new Test();
        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...


总结:

Thread.interrupt()并不会中断一个正在运行的线程。但是,如果线程被阻塞,就会抛出一个中断信号,这样线程就得以退出阻塞的状态,使阻塞状态的线程逃离阻塞状态

void interrupt():向线程发送中断。线程中断状态被设置为true.如果目前线程被一个sleep调用阻塞,那么抛出InterruptedException异常

static boolean interrupted():测试当前线程是否被中断。这是静态方法,调用会产生副作业,它将当前线程的中断状态重置为false

boolean isInterrupted():测试当前线程是否被中断,不改变中断状态

参考网址:

http://blog.csdn.net/wxwzy738/article/details/8516253


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值