what might cause the current thread to stop executing? A) An interruptedException is thrown B) The thread executes a sleep() call C) The thread constructs a new Thread D) A higher priority Thread becomes runnable E) The thread executes a read() call on an InputStream F) The current Thread call yield() G) The current Thread calls wait() H) The current thread calls notify() The given answers are A) B) D) E) F) G) I disagree with A. How this will affect the running thread? Regarding B), what if is called with sleep(0), it will not stop isn't? Regarding F), if there is no other thread, it will not stop executing. Correct me folks, if i am wrong. I know this has been asked somany times and also there are a lot of versions of this question. Which might stop thread? what may stop thread? which will stop thread? etc etc. Can anyone elobarate the answer and difference between these questions Thanks
Originally posted by Thiru Thangavelu: I disagree with A. How this will affect the running thread?
I agree. I don't think this constitues an automatic stop of the thread. A thread might terminate when it is interrupted, but that is up to the implementation of that thread. It is not true of all threads in general.
Regarding B), what if is called with sleep(0), it will not stop isn't?
In fact, calling sleep with an argument of 0 is very much like invoking the yield method. This basically states that the thread wishes to give up the processor in favor of other threads, but it can be put into the ready-to-run state immediately.
Regarding F), if there is no other thread, it will not stop executing.
Well, the thread will stop, temporarily. What will happen (this all assumes that the underlying OS honors the methods invoked by Java regarding thread management - this is not guaranteed) is that the thread will stop processing and go to the ready-to-run state. The underlying OS then chooses a thread from that state to start. As this is the only thread available, it will simply be started again. The thread does stop, however, temporarily, while this is done. I hope that helps, Corey
Thanks Corey, That helped me a lot. I guess, I can choose the answers as B) (yields and gets the processor immediately) D) E) F)yields temporarily, even if there is no other thread G) Do you think the answers I selected will do? I just want to remind you that 2 of the mock exams(don't know the name exactly) selected A) as correct answer.
My guess is that when te exams state that the thread "throws an InterruptedException," they're referring to the situation when the exception is not handled within the thread and is thrown beyond the control of the thread. An uncaught exception would cause the thread to terminate. Corey