About Question com.enthuware.ets.scjp.v6.2.671 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
mundrapiyush
Posts: 3
Joined: Fri May 15, 2015 8:04 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.671 :

Post by mundrapiyush »

Which of the following may pause/stop the current thread?
1. calling Thread.yield()
2. calling someObject.wait()
3. calling someObj.notify()
4. calling end() method on the Thread object.

Correct answer: 1,2

Please explain why '3' is not correct. Reason being: For a thread (currentThread) that's holding a lock on someObj, calling notify on currentThread may unblock other threads that were waiting for the lock on someObj and cause the current thread to move from running to runnable. Thread.yield() is correct but again its not guaranteed to put current thread to sleep.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.671 :

Post by admin »

Calling notify doesn't pause/stop the thread. It only wakes up other threads but this thread will not stop until it gets out of the synchronized block. Of course, the JVM can move any thread from running to runnable at any time as a part of its thread scheduling. No call is necessary for that.

Yield is correct because it "may" pause the thread, which is what the question is asking.

HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests