• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Threads and yield()

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following will definitely stop a thread from executing:

A. wait()
B. notify()
C. yield()
D. suspend()
E. sleep()
The answer was A, C, D, and E.
I answered A.D, and E. Does yield() really cause the thread to stop executing? I thought that it would only stop executing if the JVM chose to stop it.

Thanks.
P.S. I looked at another thread topic concerning this (which I can't find now) and I looked at several links of threads, but none seemed conclusive to me on this point.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no guarantee that calling yield() will actually do anything. In some VMs it might yield the processor to a different thread. But on a good VM running on a multi-processor system, yield() will do nothing, since the thread scheduler can do a much better job than the programmer in deciding when a thread should run or not.
So you can't say that calling yield() definately does anything.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
I think the answer should be a and e.
As you said,there is no guarantee,that yield() will definitely stop a thread from executing.
So c is not a corrent answer.
suspend() is a deprecated method,and I was told that whenever a deprecated method is included in the answer,it cannot be one of the correct answers.
So the only options which will definitely stop a thread from executing are wait() and sleep().
Am I correct?
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic