wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Why can't I stop thread??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why can Watch "Why can New topic
Author

Why can't I stop thread???

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
Hi

I have a class (static) variable Thread t.
In one method, t.start(); is executed to start thread, and this works.
In another method, t.destroy(); is executed.
But this does not work, the thread keeps on running.
Can anyone help explain this?
Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Did you look at the Javadoc for Thread.destroy() ? Note especially the part I've emphasized in boldface.


Deprecated. This method was originally designed to destroy this thread without any cleanup. Any monitors it held would have remained locked. However, the method was never implemented. If if were to be implemented, it would be deadlock-prone in much the manner of suspend(). If the target thread held a lock protecting a critical system resource when it was destroyed, no thread could ever access this resource again. If another thread ever attempted to lock this resource, deadlock would result. Such deadlocks typically manifest themselves as "frozen" processes. For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.


[Jess in Action][AskingGoodQuestions]
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
Thanks, but I don't get it.
I did check the API before, and it said Thread.stop() was deprecated,
and Thread.destroy() wasn't.
So it doesn't look like I can stop the thread, but I think I can
break out of it. Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Thread.destroy() has never been implemented in any JVM from Sun, except perhaps the Green Threads ones -- it's been a while so my memory fails me here.

In any case, use stop(), if you must, but use it only as an absolute last resort. Use interrupt() if you can, along with testing the interrupted property in your thread loop.
[ November 13, 2005: Message edited by: Ernest Friedman-Hill ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Destroy() was deprecated at the same time that stop() was deprecated. I actually would like to know what version of the javadoc / JVM, had only one of the methods deprecated.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
I use mainly two techniques to stop a thread. First, as Ernest alluded to, the interrupt() method. The interrupt() will cause the thread to throw an exception, at which point you can catch the exception and handle it in any number of ways. Specifically, you can use the catch block to kill your thread gracefully.

Second, use a global class variable which the thread will check on occasion to determine if it should still run.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

Originally posted by Jeffrey Hunter:
Second, use a global class variable which the thread will check on occasion to determine if it should still run.


I've used this technique before and I tend to prefer it. It's a painless non-interference way to terminate them, assuming your code isn't frozen or neglecting to check this.


My Blog: Down Home Country Coding with Scott Selikoff
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Edited: Earlier I said I wasn't getting the interrupted exception, but I checked my little test program and I never interrupted the thread! Now I get it.

interrupt() and interrupted() seem to be designed for exactly this purpose, so I'd prefer them for their explicit communication of what I'm trying to do. I'm using them now in something else that has several sequential steps but no sleep or anything that throws InterruptedException. It checks for interrupted() after each step.
[ November 14, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't I stop thread???
 
Similar Threads
Reservation problem!
Related to thread
I can't synchronize my threads properly
Diffrence between calling the run() method.