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
Edition》17.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的当前版本(JDK6、JDK7)的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()”。
分享到:
相关推荐
java 线程让步(Yield) java 线程让步(Yield) java 线程让步(Yield)
这篇文章主要介绍了通过实例简单了解Python中yield的作用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 介绍 我们有时候会发现代码中return的地方,有用...
二、yield是一个语法糖,为方便开发者提供的一种方便方法 三、yield返回类型为IEnumerator、IEnumerable、IEnumerator、IEnumerable 四、如果返回类型为IEnumerator编译时会实现一个实现了IEnumerator接口的类 五、...
讲解Python的yield表达式,因为这个关键字平常不常用,也容易产生歧义,不易 理解;所以这里把它单独拿出来讲解,它与return的区别,它和生成器的关系,它的通常用法。
只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: def addlist(alist): for i in alist: yield i + 1取出alist的每一项,然后把i + 1塞进去。然后通过调用取出每一项: alist = [1,...
yield curve modelling, interest rate models
使用C# yield关键字来提高性能和可读性
source code about yield curve
主要为大家详细介绍了python的yield和generator,针对python的生成器和yield关键字进行深入学习,感兴趣的小伙伴们可以参考一下
python中有一个非常有用的语法叫做生成器,所利用到的关键字就是yield。有效利用生成器这个工具可以有效地节约系统资源,避免不必要的内存占用。 一段代码 def fun(): for i in range(20): x=yield i print('good...
Pb中Yield()函数的使用[文].pdf
python库,解压后可用。 资源全名:ffmpeg_progress_yield-0.1.2-py2.py3-none-any.whl
前段时间了解到yield关键字,一直觉得还不错。今天给大家分享一下yield关键字的用法。yield return 返回集合不是一次性返回所有集合元素,而是一次调用返回一个元素。具体如何使用yield return 返回集合呢?我们一起...
主要给大家介绍了关于C#中Task.Yield的用途的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
初学 Python 的开发者经常会发现很多 Python 函数中用到了 yield 关键字,然而,带有 yield 的函数执行流程却和普通函数不一样,yield 到底用来做什么,为什么要设计 yield ?本文将由浅入深地讲解 yield 的概念和...
DESIGN FOR MANUFACTURABILITY AND YIELD FOR NANO-SCALE CMOS (English)
本文实例讲述了Python 生成器,迭代,yield关键字,send()传参给yield语句操作。分享给大家供大家参考,具体如下: demo.py(生成器,yield关键字): # 生成器是一个特殊的迭代器。可以用for...in遍历。 # 带有...
python中yield的用法全文共3页,当前为第1页。python中yield的用法全文共3页,当前为第1页。python中yield的用法 python中yield的用法全文共3页,当前为第1页。 python中yield的用法全文共3页,当前为第1页。 Python...