If you click on this stuff and give your money to some city slickers, JavaRanch gets a kickback. Hoooooooo Doggy!     
Do not post questions from the actual certification exams. If you see a question from an actual exam, please e-mail the moderators privately to let us know. To discuss a question from a mock exam, please mention which mock exam and the question number (if provided) so we know it's not from the real exam.



UBBFriend: Email This Page to Someone!
  JavaRanch Big Moose Saloon
  Programmer Certification
  Threads

Post New Topic  Post A Reply
profile | register | preferences | faq | search

next newest topic | next oldest topic
Author Topic:   Threads
Dadi
greenhorn
posted March 12, 2000 02:58 PM     Click Here to See the Profile for Dadi   Click Here to Email Dadi     Edit/Delete Message Reply w/Quote
Question: Can a thread be stopped by interrupt().

R&H mentions that;
i) A thread can be killed from another thread by interrupt().

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     Click Here to See the Profile for maha anna   Click Here to Email maha anna     Edit/Delete Message Reply w/Quote
There are 3 interrupt related methods in Thread class.

1. void interrupt()
This instance method is to interrupt a particular thread.
ex. myT.interrupt(); where myT is a ref to a thread object. Any thread can be interrupted by any other thread provided it has access to that thread .For every thread there is a status flag which indicates whether that thread is interrupted by an another thread. The consequences of interrupting a thread differs depending upon what the target thread (on which interrupt() is invoked, here the 'myT' thread) is doing. If it is sleeping/ or waiting for some resource (either an object lock/file data from remote server.. etc), then it would have been in the try..catch(InterruptedException e) statement, provided compiler has done its part correct , and as soon as you interrupt this thread, InterruptedException is thrown by JVM and this target thread catches this Exception and comes out of waiting state and goes to Ready state. The JVM immediately resets the interrupt status flag of this thread, because the action for the interrupt is already taken.

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()
methods are used to check the interrupt status flag.

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 .
So interrupt() does not kill the target thread. It can be used to kill the target thread.

regds
maha anna

[This message has been edited by maha anna (edited March 12, 2000).]

IP: Logged

Dadi
greenhorn
posted March 12, 2000 08:07 PM     Click Here to See the Profile for Dadi   Click Here to Email Dadi     Edit/Delete Message Reply w/Quote
Thanks Anna for the elaborate reply.

IP: Logged

All times are MT (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | JavaRanch

Powered by: Ultimate Bulletin Board, Version 5.44a
Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 2000.