On 5/30/2010 2:25 PM, Marcin Rodzik wrote:
On May 29, 9:42 pm, Eric Sosman<esos...@ieee-dot-org.invalid> wrote:
Thread.sleep()
Thread.yield()
Both techniques are *vastly* inferior to wait().
So whay does yield() exist? Is there any case in which it can be
preferred?
I can't think of a reason to use yield(), unless you happen to
be writing Java for one specific platform and you know a lot about
how that platform schedules threads. That is, I cannot think of a
way to use yield() and get the same effect on multiple platforms.
In short, I can't think of a portable reason to use yield() --
maybe somebody smarter than I am knows of one, but I don't. As to
why it exists, I don't really know but I surmise the Java designers
decided to incorporate most of the POSIX Pthreads facilities, and
Pthreads has yield() analogs (with similar drawbacks).
That sounds like a good explanation. I would have offered the guess
that maybe some Java mobile platforms need Thread.yield(). I can't
think of another modern platform that would require Thread.yield().