Java 线程的sleep和yield方法

yield() method pauses the currently executing thread temporarily for giving a
chance to the remaining waiting threads of the same priority to execute.

yield方法临时停止当前线程的执行,给其他的和当前线程同样优先级的线程一个执行的机会。

If there is no waiting thread or all the waiting threads have a lower priority
then the same thread will continue its execution.

如果在调度池中没有等待的线程或者所有等待的线程的优先级没有调用yield线程的优先级高,当前线程继续执行。

The yielded thread when it will get the chance for execution is decided by the
thread scheduler whose behavior is vendor dependent.

什么时候执行已经yield的线程是由线程调度器来决定的,这和JVM的实现有关。

sleep() causes the current thread to suspend execution for a specified period.

sleep方法使得当前执行的程序停止一段时间,这时线程会让出CPU来给其它的线程。

This is an efficient means of making processor time available to the other
threads of an application or other applications that might be running on a
computer system.

这样可以使得同一个程序的线程或者别的程序有CPU时间来执行自己。

The sleep method can also be used for pacing and waiting for another thread
with duties that are understood to have time requirement.

sleep方法也可以用来等待其它线程,但是有时间限制罢了。

sleep method in Java has two variants one which takes millisecond as sleeping
time while other which takes both mill and nano second for sleeping duration.

sleep方法有两个重载的方法,一个用毫秒作为参数,另外一个用毫秒和纳秒作为参数。

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

http://www.tkhts.com/core-java/multithreading/java-thread-scheduler.jsp

PS:

Java Thread Scheduler
The JVM is based on Preemptive and priority based scheduling algorithm .

The thread with more priority is given first preference than the thread with
less priority.

The thread with more priority relinquishes (empties) the thread with less
priority that is being executed. ?

If the threads of equal priority are in the pool, the waiting time is taken in
consideration.

Nature of threads sometimes affects. The daemon threads are given less
importance and are executed only when no other thread is available for
execution.