This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Question" Watch "Threads Question" New topic
Author

Threads Question

Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
SCJP app version 1.11


which method of java.lang.Thread promises to cause the current thread to pause for a undefined moment in order to allow other threads to execute?

1.yield
2.sleep
3.suspend

the answer is yield.
But I don't understand how come yield PROMISES other threads to execute? the same thread might start executing again... Isn't it true?
Please clarify on this. Thank you.
Tom Godfrey
Greenhorn

Joined: Apr 10, 2012
Posts: 3
yield() doesn't "PROMISE" other threads to execute. yield() promises to give other threads an OPPORTUNITY to execute by returning the thread back to the runnable state allowing a new thread to run. The thread chosen to run is from the group of threads waiting in the runnable state which the original thread is now a part of. So it is very possible that the original thread may be chosen to continue running.

From K&B - In reality, though, the yield() method isn't guaranteed to do what it claims, and even if yield() does cause a thread to step out of running and back to runnable, there's no guarantee the yielding thread won't just be chosen again over all the others.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
In my opinion, I think the answer should be sleep. The current thread will sleep and let other threads execute.

But why the answer is yield?

Anyone can help?
Tom Godfrey
Greenhorn

Joined: Apr 10, 2012
Posts: 3
Just guessing so correct me if I'm wrong but it's yield() because of "pause for a undefined moment" as stated in the question.

The Thread class has 2 overloaded versions of the sleep() method:

static void sleep(long millis)
static void sleep(long millis, int nanos)

so using the sleep() method would be pausing for a DEFINED moment in time where as yield() doesn't have any time parameters and can be used as an "undefined moment."

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, Tom. Thanks for your explanation. But yield won't promise that the current thread will yield other threads. The current thread may execute until it is finished without yielding, just as the previous post said.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Helen Ma wrote:In my opinion, I think the answer should be sleep. The current thread will sleep and let other threads execute.

But why the answer is yield?

Anyone can help?


Hi Helen,
I thought that too. sleep should be the answer.
In K&B book it says "Using sleep() is the best way to help all threads get a chance to run! Or at least to guarantees that one thread doesn't get in and stay until it's done.

Can anyone help clarifying this?
Tom Godfrey
Greenhorn

Joined: Apr 10, 2012
Posts: 3
Hi Helen and Janki. I think it depends on how the question is interpreted and what terms are important. The key terms being promises and undefined moment.

Your both correct that 'the same thread might start executing again' or that the JVM can ignore the yield() and continue running the thread.

I think its a poorly written question but in terms of the question (as I interpret it), I still think yield() is the correct answer.

Good chance I'm wrong with this but the way I see it is:

yield() does 'promise' to pause. Whether the JVM responds to that yield() or not is up to the JVM. Doesn't mean that it's going to happen, just that it promises to yield for other threads if the JVM wants to take advantage of it. Whereas the sleep() method essentially guarantees that it will take a break as Janki stated from the book.

The other term would be how you read 'undefined moment.' Does that mean some amount of time that you just haven't told about (N) or does that mean a method that doesn't require a parameter of time? I'm reading it as the latter which would fit the yield() method versus an undefined amount of time (N) that would be used as a parameter for the sleep() method.

Again, I think its a poorly written question that is open to interpretation and I could easily defend sleep() being the correct answer. I feel bad for suspend(). RIP.





 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads Question
 
Similar Threads
does yield prevent thread execution?
Q on thread at Jxam
Marcus Mock Question 24
thread priority!
qstns from Java2-philip Heller & others