• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads Question

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.





 
reply
    Bookmark Topic Watch Topic
  • New Topic