Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly stop a thread, if my call to Thread.interrupt() will not work? [duplicate]

It is a widely known fact that one shall not stop running processes using Thread.stop().

Usually the manuals and tutorials suggest using Thread.interrupt() or some boolean variable instead, and checking from inside the code for that interrupt or variable.

But if I have a library method which takes a very long time to execute sometimes, and I want to give user an ability to stop that process? And library does not give me a mechanisms to do it (does not check thread interrupted status, and no "stop!" variables)?

And, to add to the bad things, there is either no source code for library, or it is just too big to edit it and add checks at appropriate places.

It seems that Thread.stop() is the only solution here. Or maybe there is some workaround?

like image 866
Rogach Avatar asked Apr 26 '11 17:04

Rogach


People also ask

What happens when you call interrupt () on a running thread?

In Java Threads, if any thread is in sleeping or waiting state (i.e. sleep() or wait() is invoked), calling the interrupt() method on the thread, breaks out the sleeping or waiting state throwing InterruptedException.

Does thread interrupt stop thread?

interrupt() does not interrupt the thread, it continues to run.

Which of the following method will cause a thread to stop invoking the interrupt of the thread?

C. Explanation: Option A is correct. wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.


4 Answers

If practical in your situation, spawn another process and treat that as the unit of work, rather than a thread. Process killing is much more deterministic, though devoting a process to what used to be a thread's work might be too heavyweight for your situation.

like image 30
jlew Avatar answered Nov 15 '22 01:11

jlew


Why you do not try to use sleep(long timeout) when are waiting for some condition and if had not success, you simple "return" from the thread?


Probably your thread is running in a while (booleanVariable) { },

if it is, you could set this variable as volatile, and the thread controller set it as false.

Think of the Thread.stop() like the System.exit(value), it works, but when you have some bug making you thread stop/vm exit, will be much more harder to find it out.

like image 112
Pih Avatar answered Nov 15 '22 00:11

Pih


The only solution better than using Thread.stop() is to use the library in a seperate thread which you can kill to stop it.

like image 41
Peter Lawrey Avatar answered Nov 15 '22 00:11

Peter Lawrey


You may want to look for different handles of the function you are running, for example if its IO you can try to close any open connections/streams. If you are stuck with this library (IE can't find one that has better interruption mechanics) Thread.stop() is your only way of stopping the thread.

like image 31
John Vint Avatar answered Nov 15 '22 00:11

John Vint