Dave
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Amit Agrawal*:
what is the best way to kill a thread ? ok, I have few child threads which don't hold any resources but are user threads (Actually Timer threads).
I want to kill all of them at some point so that my parent thread can end. since stop() is Deprecated, what could be the best way to kill a tread.
I was looking at destroy() method but it has not been implemented so far (jdk1.4 beta).
Originally posted by Roseanne Zhang:
[b]Dr. Haan
I might missing something. How could Thread.interrupt() equivalent to kill the Thread? Can you give a simple example?
B]
Originally posted by Roseanne Zhang:
[b]Dr. Haan
I might missing something. How could Thread.interrupt() equivalent to kill the Thread? Can you give a simple example? ...
Roseanne, to my mind it depends. If you need to do something with a Thread that should interrupt what it is currently doing and make it do something else, you should use interrupt(). That's what it is for.Originally posted by Roseanne Zhang:
2) CL, you're right too. I read the other post of Peter Haan, and now understand what he meant by using interupt. I still don't agree with him since interrupt is too generic, it can be set and unset for many reasons, not exclusively for kill. My runFlag is better.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Peter den Haan:
Roseanne, to my mind it depends. If you need to do something with a Thread that should interrupt what it is currently doing and make it do something else, you should use interrupt(). That's what it is for.
If there are multiple things you may want a thread to do, then a flag is the only way forward. Set the flag, call interrupt() to wake up the thread from whatever it is doing. It will read the flag and do what it's supposed to do.
If there's only one thing you ever want a thread to interrupt its work for - in this case: die - then a flag is, in principle, superfluous. You could perhaps argue that it should be retained nevertheless for clarity. But if clarity is the issue then a much better solution would be to create a proper abstraction of the whole thing and provide a management interface with a killThread() method. Behind the scenes, that killThread() method may simply be calling interrupt(); this is then a hidden implementation detail and the "too generic" argument no longer applies.
- Peter
Originally posted by Jim Baiter:
But runflag has package scope. Couldn't another class in the same package change the runflag to false thereby causing the run method to complete and the thread to terminate?
Taken at face value, this is incorrect. It will (as you well know) set the interrupted flag which can be queried using the isInterrupted() or interrupted() methods. The flag exists specifically so you can check it inside tight loops that won't otherwise be interrupted. For instance, you could simply incorporate in a suitably frequently executed spotand handle the exception at the appropriate level. If you've got any wait()s or sleep()s this nicely unifies the way you handle interrupts.Originally posted by CL Gilbert:
If you are off processing packet data or anything that the thread is not wait() or sleep() then interrupt will do nothing.
That's an oversimplification - notify() and interrupt() are completely different means to completely different ends. Most likely, notify() is used to coordinate the normal flow of operation within a class - a producer/consumer pattern, whatever. Interrupt() on the other hand is typically used to break the normal flow of operations. Code such asmixes up the normal logic with a special case (!isRunning) and is - especially if you need to break out of several layers of blocks and method calls - a more difficult to understand than using an interrupt and letting the exception propagate up to the level where you can catch it and act upon it (in this case, you can catch it just inside the run() method and simply allow control to drop out of run() to stop the thread).In the wait() case, you should be able to notify the thread
That's obvious, but does not mean that it isn't a fine way to implement the required thread-stopping feature. By the way, if you scroll up in the thread, you'll see that I recommended daemon threads as a first choice.But that is the only case I can think of. Interrupt does not stop the thread, I hope you see that point.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
I see your point; actually, I don't think we're quite as far apart as it seems.Originally posted by CL Gilbert:
Peter, I just think using interrupt to "stop" the thread is not correct. [... Thread class with 3 states ...]In my opinioin this is how you handle manipulating a running thread. Interrupt has its place, but I don't think this is it.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
dan moore, infomatiq ltd.<br />email dan@infomatiq.co.uk<br /><a href="http://www.infomatiq.co.uk" target="_blank" rel="nofollow">http://www.infomatiq.co.uk</a>
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
dan moore, infomatiq ltd.<br />email dan@infomatiq.co.uk<br /><a href="http://www.infomatiq.co.uk" target="_blank" rel="nofollow">http://www.infomatiq.co.uk</a>
Every plan is a little cooler if you have a blimp. And a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|