Reputation: 25028
Well, I have a thread in my video converter which is responsible for transcoding the video. It is a user thread
with setDaemon(false)
.
To stop it, I call the threadName.interrupt()
method however, it does not stop. It continues on !
How do I stop it?
Here is how I try to stop it:
if(getExecutingTaskID() == taskID){
int what = JOptionPane.showOptionDialog(frame,
"Do you want to interrupt an executing task?",
"Task already running",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, null, null);
if(what == JOptionPane.YES_OPTION){
if(getExecutingTaskID() == taskID){
converter.interrupt();
}
return true;
}
}
return false;
}
Upvotes: 2
Views: 617
Reputation: 48211
Quoting from the Java Tutorials (emphasis mine):
"An interrupt is an indication to a thread that it should stop what it is doing..."
"It's up to the programmer to decide exactly how a thread responds to an interrupt..."
"For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption..."
Essentially, you have to watch for the "Interrupted" flag from within the thread and terminate it "from inside" when you detect the flag has been set. There are several ways to check for and handle interruption (see the link above) - e.g. you could use Thread
's interrupted() method
.
I don't know the inner workings of your readers and converters, but this while-loop
seems like a good canditate for implementing the check:
while (reader.readPacket() == null) {
// code the next packet
...
// Check for interruption
if (Thread.interrupted()) {
// We've been interrupted: terminate
return;
}
}
Upvotes: 3
Reputation: 29964
A thread can only be interrupted when it calls a method that throws InterruptedException
or if it calls Thread.interrupted
and is coded to respond to it. If you want to be able to break your thread at any time, you'll need to build that into the code itself. Oracle provides a tutorial. Basically, your options are to use methods that throw InterruptedExcetion
like sleep
or wait
, to periodically call Thread.interrupted
, or have your own stop variable that you check periodically.
No matter how you slice it, you have to implement the interrupt. Java just has some simple ways of getting the message to the thread.
There is a Thread.stop
method, but this method is considered extremely dangerous. This stops the thread and immediately releases all locks, which may leave objects in an unusable state. Using it is generally just a bad idea.
(Sorry to the major revamp of the answer.)
Upvotes: 4
Reputation: 68715
There are 3 ways to terminate a thread:
- The thread has finished the work it is designed to do, and exits the run() method naturally.
- The thread has received an interruption signal, while doing its work. It decides to not continue with work and exits the run() method. It may also decide to ignore the signal and continue to work.
- The thread has been marked as daemon thread. When the parent thread who created this thread terminates, this thread will be forcefully terminated by the JVM.
See this thread by Sun on why they deprecated Thread.stop(). It goes into detail about why this was a bad method and what should be done to safely stop threads in general.
http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
The way they recomend is to use a shared variable as a flag which asks the background thread to stop. This variable can then be set by a different object requesting the thread terminate.
Upvotes: 2