深入了解Thread#yield


Thread#yield方法表示“暂停当前正在执行的线程对象,并执行其他线程”。在《The Java Language Specification, Third Edition》的17.9 Sleep and Yield 一节中是这样描述的:

 

Thread.sleep causes the currently executing thread to sleep (temporarily cease execution) for the specified duration, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors, and resumption of execution will depend on scheduling and the availability of processors on which to execute the thread.

Neither a sleep for a period of zero time nor a yield operation need have observable effects.

 

It is important to note that neither Thread.sleep nor Thread.yield have any synchronization semantics. In particular, the compiler does not have to flush writes cached in registers out to shared memory before a call to Thread.sleep or Thread.yield, nor does the compiler have to reload values cached in registers after a call to Thread.sleep or Thread.yield.

 

 


而在《The Java Language Specification, Java SE 7 Edition17.3. Sleep and Yield 一节中是这样描述的:

 

Thread.sleep causes the currently executing thread to sleep (temporarily cease execution) for the specified duration, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors, and resumption of execution will depend on scheduling and the availability of processors on which to execute the thread.

 

It is important to note that neither Thread.sleep nor Thread.yield have any synchronization semantics. In particular, the compiler does not have to flush writes cached in registers out to shared memory before a call to Thread.sleep or Thread.yield, nor does the compiler have to reload values cached in registers after a call to Thread.sleep or Thread.yield.

 

 

对比新版的JLS与旧版的JLS,少了句“Neither a sleep for a period of zero time nor a yield operation need have observable effects

 


这里有一些“说明”:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6584700

 

Section 17.9 "Sleep and Yield" in the Java Language Specification Third Edition, has the following statement:

 

  "Neither a sleep for a period of zero time nor a yield operation need have observable effects."

 

This precludes java.lang.Thread.yield from having any strong specification as it could be challenged as a contradiction to this statement. Effectively this statement in the JLS allows yield to be a noop and prevents it from gaining any strongly specified behavior.

 

请教了下RednaxelaFX,他这么说的:“范里去掉这句话也没保证sleep(0)yield()一定有效果,只是懒得被人挑刺而已吧”。

 



JDK1.6之前的javaDoc中都是这么写的:

 

public static void yield()

Causes the currently executing thread object to temporarily pause and allow other threads to execute.

 

 

 

在新版的JavaDoc中,做了修正:

 

public static void yield()

A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint.


Yield is a heuristic attempt to improve relative progression between threads that would otherwise over-utilise a CPU. Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.

It is rarely appropriate to use this method. It may be useful for debugging or testing purposes, where it may help to reproduce bugs due to race conditions. It may also be useful when designing concurrency control constructs such as the ones in the java.util.concurrent.locks package.

 

描述的更清晰易懂了。

 


同时,RednaxelaFX 还提到:“HotSpot VM的当前版本(JDK6JDK7)的Linux版里的Thread.yield()实现默认跑到最底下是sched_yield()http://www.kernel.org/doc/man-pages/online/pages/man2/sched_yield.2.html;同版本里Thread.sleep(0)会转换为跟Thread.yield()等价的行为,也是调用到sched_yield()”。

你可能感兴趣的:(thread)