Skip to main content

Thread Problem

4 replies [Last post]
Joined: 2007-11-24

Dear All,

I am new to thread. I am using thread for my application, I want to stop the thread in certain condition. Since Thread.stop() is deprecated. There must me other way to stop the Thread. Can anybody help me please. Thanks in advance.


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-12-18

As it stands, and has been said, stopping threads dead in their tracks was deprecated as it potentially leads to severe problems, the most prevalent being deadlocking. And I concur that the first suggestion is a textbook example of how not to attack this problem.

As jlrowe said, there is a fairly recent package giving advanced features to deal with concurrency, namely java.util.concurrent and its subpackages. There are far better books and articles out there than I could possibly put together. But it is also a quite advanced topic, and you're perhaps not ready to go there.

Thread.interrupt() is a powerful tool. This works if your thread is blocked on an interruptible operation like Object.wait(). But the easiest way remains to build your libraries/application so that it is easily interruptible. Consider the following (contrived) example:

[code]void copy(Collection source, Collection destination) {
for (Object o : source) {
if (Thread.isInterrupted())

However, if your thread is blocked on a non-interruptible operation, then there is typically nothing you can do. Perhaps another thread can unblock you by, say, closing a stream you're reading from if the stream so allows. But usually, it's out of your hands.

So, basically, if the code you invoke is not built to handle interruptions, there's nothing you can do about it. Think about this when you go about writing potentially blocking code of your own.

Hope this clears things up for you,

PS: The java.nio package supplies classes to work with non-blocking I/O. If you're trying to interrupt a blocking read from an inputstream, consider checking out [i]channels[/i]; those can be configured not to block.

Joined: 2008-02-20

Hi Manoj!

You are not the first and will definetively not the last user who will become angry about thread handling in Java. This is because of a missing advanced implementation for threads and the lack of documentation.

Currently the only way to stop a thread is Thread.interrupt(). You have to implement a try-catch which encapsulates the whole thread code.

.... // the complete thread code
catch(Exception ex)

For further details have a look at


Message was edited by: kirsche40

Joined: 2005-06-27

Please ignore the last posters oversimplistic explanation.

Look at java.util.concurrent (advanced features) and look at the book java concurrency in practice (an excellent book on a very tricky subject).

There's a reason that concurrency is tricky in java, that's because it's a tricky subject.

java.util.concurrent provides you with some abstractions that can make the whole thing a lot easier, but nothing is going to totally shield you from the fundamental difficulties of concurrency.

Joined: 2006-05-17