|
UBBFriend: Email This Page to Someone! |
JavaRanch
Big Moose Saloon Programmer Certification Threads
|
next newest topic | next oldest topic |
Author | Topic: Threads |
Dadi greenhorn |
posted March 12, 2000 02:58 PM
Question: Can a thread be stopped by interrupt(). R&H mentions that; ii) A sleeping thread recieves an interrupt() and moves into Ready state. The above appear to be contradicting each other. Can anyone clarify the same. Thanks. IP: Logged |
maha
anna bartender |
posted March 12, 2000 03:53 PM
There are 3 interrupt related methods in Thread class. 1. void interrupt() But, if the target thread is not in a checking InterruptedException state, assuming it is doing something else, when it is interrupted, it doesn't even know that it was interrupted by another thread. So here the effect is different. Maximum the JVM can do is just set the status flag of the target thread to be true. It is upto the target thread to check the status, and take some action if at all it wants to respond to this disturbance .( ). The 2. static boolean interrupted(),3. boolean
isInterrupted() So, having said all the theroy part, when we invoke the stop() method on any thread, the target thread immediately releases all the locks which it had held, which is not at all good. Because the original thread which stopped this target thread ,does not have any idea of what this target thread is doing, whether it is good idea to stop at this time or not. Since the stopped thread just releases all the locks, it may leave the held objects in an inconsistant state ,and all other threads which are going to use these held objects from now on, may see some abnormal behaviour. This is the reason why the stop() method is deprecated. So, the good idea to kill a thread is set some boolean hasToDie
flag to be true (it may be a member of the target thread or member of
another mediator object. It doesn't really matter) and then call the
interrupt() method on the target thread. In this kind of killing method,
both threads should be co-operative for the killing to be success. The
target thread MUST periodically check the boolean hastoDie var and
has to come out of the running state in a smooth way when this
hastoDie flag is set to true by another thread indicating that it's
time to RIP . regds [This message has been edited by maha anna (edited March 12, 2000).] IP: Logged |
Dadi greenhorn |
posted March 12, 2000 08:07 PM
Thanks Anna for the elaborate reply. IP: Logged |
All times are MT (US) |
next newest topic | next oldest topic |
|
Powered by: Ultimate Bulletin Board, Version 5.44a
© Infopop Corporation
(formerly Madrona Park, Inc.), 1998 -
2000.