Which methods may cause a thread to stop executing? sleep(); stop(); yield(); wait(); notify(); notifyAll() synchronized() I feel the answer is 1,2,4 But given answer is 1,2,3,4 I think yield will stop the thread only if the other high priority thread or equal priority thread is waiting. If no threads are in waiting the yield method executes the current thread. But look the other question which of these will always cause a running thread to temporarily stop? A)Calling a sleep() B)calling yielsd() C)The run() calling system.exit(0) D)starting another thread with a higher priority thread E)None of the above Answere is a I do agree with this Any other answrs Thanks in advance
The difference is that the first question says may cause a thread to stop executing. As you rightly say, calling yield may stop the thread if another thread is scheduled to start (this depends on the scheduler). In the second question it says, will always cause a running thread to temporarily stop. sleep() is the only thing listed that does this. It's all in the wording of the question
In the question : Which of these will always cause a running thread to temporarily stop? A)Calling a sleep() B)calling yielsd() C)The run() calling system.exit(0) D)starting another thread with a higher priority thread E)None of the above Why is that only A is the correct answer. D would also cause the current thread to stop temporarily. Is it not so ?
A synchronized method or a synchronized block *may* stop a thread from executing if another thread already has the monitor for the object in question. Is the reason that item #7 of the first question (synchronized()) not a valid answer because the question asked specifically about which "methods" may cause a thread to stop, and synchronized() is not really a method? Seems like a subtle play of words.
Joined: Jan 07, 2002
Ok, I answered my own question 3 minutes later. "synchronized" is a modifier, not a method. silly me