Author Topic:   Oh boy !!!! Threads again.
ranch hand
posted March 20, 2000 06:03 PM             
Hello All,

I found this question in one of the mock exams. The question is

Which is true about threads?
a.If suspended cannot be restarted
b.Stop running,if another thread with higher priority is getting runnable.
c.Stop running, if the thread starts another thread with the same priority.
d.Stop running, if the thread creates another thread.
e.If interrupted, stop running.
f.If dead cannot be restarted.

The answer given is b,c,e,f.

I think only b&f are correct.According to my knowledge interrupt() only wakes up the waiting or sleeping thread and moves it into a ready state. Does it stop the executing thread. If the interrupt() is called on a thread which doesn't contain it's wait()or sleep() in a try & catch block will it stop the executing thread because of the thrown InterruptedException.


Please help me i am totally stuck at this point and i can't move forward.

Bytheway is there any excellent online tutorial available regarding threads.

Alkesh Nandanwar
ranch hand
posted March 20, 2000 09:07 PM             
hi sree
you are perfectly right. The correct answers in my opinion are b) and f)
a)As suspended thread can always be restarted(though suspend method is deprecated in java2)
c)and d)The currently running thread will not stop running if it create another thread,irrespective of the priority of the thread.
e)Interrupt doesn't cause thread to stop.
f)Dead thread can not be restarted again, though we can use the methods of it.
Hope my wording is correct

ranch hand
posted March 20, 2000 09:32 PM             
Thanks alk.

Maha Anna i still need an explanation from you regarding interrupt(). So, interrupt() doesn't stop a running thread???

Alkesh Nandanwar
ranch hand
posted March 20, 2000 09:43 PM             
hi sree,
maha anna explained regarding the interrupt() few days back, u can use the search facility at the top of this page and search for interrupt keyword.
I belive u will find lot of posts

maha anna
posted March 21, 2000 01:59 AM             
I was out for some time. Regarding the interrupt, the statement If interrupted, stop running. is not true. We can not generalize the statement like that. What happens to the interrupted thread depends on what it was doing when interrupted. Depending upon that the end result varies. If the interrupted thread would have been in a waiting state, then an InterruptedException will be generated because of the interrupt() on this thread and the interrupted thread comes out of the waiting/blocked state to Ready State. On the other hand if it wasn't in a wait state and was executing some statements then the effect is different. So the above statement is not always true. Why I am saying 'always' is , the interrupted thread may want to respond to the interruption. It may provide a smooth way of going to dead state. But this happens only if it wants to.

You may find this discussion useful.

maha anna

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

ranch hand
posted March 21, 2000 08:58 AM             
Thank you very much Anna.