java线程:Thread.interrupt()不起作用 [英] java thread: Thread.interrupt() not working

查看:1123
本文介绍了java线程:Thread.interrupt()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要杀死未在代码中创建的线程.换句话说,线程对象是由api(Eclipse JFace)创建的.这是我的代码

I need to kill a thread that is not created in my code. In other words, the thread object is created by api (Eclipse JFace). Here is my code

ProgressMonitorDialog dialog = new ProgressMonitorDialog(null);
try {
        IRunnableWithProgress rp = new IRunnableWithProgress(){

            @Override
            public void run(IProgressMonitor monitor)
                    throws InvocationTargetException, InterruptedException {
                Thread.sleep(3000);

                Thread t = Thread.currentThread();
                t.getThreadGroup().list();

                t.interrupt();
            }
        };
        dialog.run(true, true, rp);
}
catch (Exception e) {
    e.printStackTrace();
}

Thread.currentThread()返回名称为"ModalContext"的线程.第t.getThreadGroup().list()行返回以下数据:

Thread.currentThread() returns a thread with the name "ModalContext". Line t.getThreadGroup().list() returns the following data:

...
Thread[qtp1821431-38,5,main]
Thread[qtp1821431-39,5,main]
Thread[qtp1821431-40,5,main]
Thread[qtp1821431-42 Acceptor0 SelectChannelConnector@0.0.0.0:18080,5,main]
Thread[DestroyJavaVM,5,main]
Thread[ModalContext,5,main]

变量"dialog"和"rp"没有引用其可运行对象.而且他们没有任何方法可以关闭或取消.因此,我想直接杀死该线程"ModalContext".调用t.interrupt()不起作用.线程MoadlContext继续运行.我如何杀死线程?谢谢

Variables "dialog" and "rp" do not have reference to their runnable object. And they don't have any method to close or cancel. So I want to kill that thread "ModalContext" directly. Calling t.interrupt() does not work. Thread MoadlContext continues to run. How can I kill the thread? Thanks

推荐答案

t.interrupt()并不会立即立即中断线程,它只会更新线程的中断状态.如果您的线程包含仅轮询中断状态(即sleep)的方法,则该线程将被中断,否则该线程将简单地完成执行,并且中断状态将被忽略.

t.interrupt() does not actually interrupt the thread immediately it only update interrupt status of thread. If your thread contains method which poll the interrupt status (i.e. sleep )only then the thread will be interrupted otherwise the thread simply complete the execution and interrupt status will be ignored.

考虑以下示例,

class RunMe implements Runnable {

    @Override
    public void run() {
        System.out.println("Executing :"+Thread.currentThread().getName());
        for(int i = 1; i <= 5; i++) {
            System.out.println("Inside loop for i = " +i);
        }
        System.out.println("Execution completed");
    }

}

public class Interrupted {

    public static void main(String[] args) {
        RunMe runMe = new RunMe();
        Thread t1 = new Thread(runMe);
        t1.start();
        t1.interrupt();//interrupt ignored
        System.out.println("Interrupt method called to interrupt t1");
    }
}

输出

Interrupt method called to interrupt t1
Executing :Thread-0
Inside loop for i = 1
Inside loop for i = 2
Inside loop for i = 3
Inside loop for i = 4
Inside loop for i = 5
Execution completed

现在只需在run中添加Thread.sleep(200);,您就会看到InterruptedException.

Now just add Thread.sleep(200); in run and you will see the InterruptedException.

这篇关于java线程:Thread.interrupt()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆