File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Stop the Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Stop the Thread" Watch "Stop the Thread" New topic

Stop the Thread

Roger Zhao
Ranch Hand

Joined: Aug 05, 2003
Posts: 73
Hi,all,here are two questions about stop thread,
[Q1]Which two can directly cause a thread to stop executing? (Choose Two)
A. Exiting from a synchronized block.
B. Calling the wait method on an object.
C. Calling the notify method on an object.
D. Calling the notifyAll method on an object.
E. Calling the setPriority method on a thread object.
choose BE
"Stop" here means blocked, right? B is correct.
What about setPriority happend on stoping action???
Which two CANNOT directly cause a thread to stop executing? (Choose Two)
A.Calling the yield method
B.Calling the wait method on an object
C.Calling the notify method on an object
D.Calling the Notify All method on an object
E.Calling the start method on another Thread object
choose A.E
Yield method causes the thread from running to ready.
E is nothing to do to stop currect thread executing.
Who give me a clear about BCD for this question?
Thanks a lot,

"There is a will,there is a way!"<br />SCJP1.4
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi Roger:
Q1- setPriority method in a thread Object will usually make the lower priority running thread bumped back and the highest priority thrad will be chosen to run. but there is no guarantee.
Q2- wait() when invoked on an object, the thread excuting that code give up its lock IMMEDIATELY.
notify() and notifyAll() doesn't not guarantee that the thread will give up the lock immediately.

SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Roger Zhao
Ranch Hand

Joined: Aug 05, 2003
Posts: 73
So,what is the right answer for the question above?
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
hi roger,
about your first question i believe you are right, the answers are B & E although i can't quite explain the choice E. i solved it simply because the other choices doesn't make sense.
i have doubts about the answers you've provided for Q2, i'll go with C & D because:
A) yield is a static method when executed will pause/stop the current thread and choose another one from the waiting list.
B)wait() will cause the current thread to stop executing until the object that wait() executed on finishes.
E) calling the start method on any other thread will put that thread in the runnable state.
therfore i do believe the answers are C & D.
i am not sure about my answers, if somebody doesn't agree please explain.
Hope this helps.

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Vicken
I agree with Hanna's explaination about the Q2 answer and that seems clear to me. The question asks "which CAN NOT directly stop" and as per Hanna pointed, notify() and notifyAll() doesn't GUARANTEE to stop the thread. It may or may not. So, we can't say for certain that "it can't directly stop" as it "may". You know what I mean?
Now, for the 1st question's option E- setPriority() method will change the priority of a thread and that causes the system to reconsider the thread execution depending upon the thread scheduling policy. In order to do that system has to stop execution of that thread. Its like if we are shifted from developer's position to manager's we will have to put stop on pure developer mindset and start focus on manager mindset and that causes us to 'stop' for a while
I agree. Here's the link:
subject: Stop the Thread
It's not a secret anymore!