It depends on what Java version you are using, according to this:
In Java 5, Thread.yield()
calls the Windows API Sleep(0)
. This has
the special effect of clearing the current thread's quantum (number of
allocated time-slices of CPU) and putting it to the end of the queue
for its priority level. In other words, all runnable threads of the
same priority (and those of greater priority) will get a chance to run
before the yielded thread is next given CPU time. When it is
eventually re-scheduled, it will come back with a full quantum, but
doesn't "carry over" any of the remaining quantum from the time of
yielding. This behavior is a little different from a non-zero sleep
where the sleeping thread generally loses 1 quantum value (in effect,
1/3 of a 10 or 15ms tick).
In Java 6, this behavior was changed. The Hotspot VM now implements
Thread.yield()
using the Windows SwitchToThread()
API call. This
call makes the current thread give up its current time-slice, but not
its entire quantum. This means that depending on the priorities of
other threads, the yielding thread can be scheduled back in one
interrupt period later.
Thread.sleep()
suspends the current thread for a specified time, no matter what Java version you use.