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() 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]
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()
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.Consider following example,
OUTPUT
Now just add
Thread.sleep(200);
inrun
and you will see theInterruptedException
.The
interrupt
method doesn't kill the thread. It sets the "interrupted" status on theThread
, and if it's sleeping or waiting on I/O, then that method that it's calling will throw anInterruptedException
.However, you call
interrupt
on the current thread aftersleep
finishes, so this will do nothing but set the "interrupted" status.You can do one of the following:
Thread
callinterrupt
on thatThread
. Inrun()
, let the method complete if anInterruptedException
is caught or ifinterrupted()
returnstrue
.volatile boolean
variable (say,isRunning
) that is initialized totrue
in the created thread. That thread will let therun()
method complete if it'sfalse
. Have anotherThread
set it tofalse
at the appropriate time.