JDK1.5.0的API文档里的描述:
yield:Causes the currently executing thread object to temporarily pause and allow other threads to execute.
sleep:Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
根本无助于理解两者间的差别
线程的生命周期里有三个状态Runable、Blocked、Running
yield:Running -> Runable
sleep: Running -> Blocked -> Runable
yield和sleep都是在线程处于Running的时候开始的,yield只是让出分配给自己的CPU时间片,并且会立刻进入Runable状态参与CPU时间的竞争,若程序中没有其他线程,那么该线程马上就会开始往下执行;sleep会进入Blocked状态,等待时间结束事件的发生,然后进入Runable状态参与CPU时间的竞争
另外,sleep和yield都不具备同步语义,也就是说编译器在执行sleep或yield方法之前和之后,都没有强制要求同步本地缓存与主存的数据
以下摘自JSL3.0
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.
For example, in the following (broken) code fragment, assume that this.done is a non-volatile
boolean field:
- while (!this.done)
- Thread.sleep(1000);
The compiler is free to read the field this.done just once, and reuse the cached value
in each execution of the loop. This would mean that the loop would never terminate, even if
another thread changed the value of this.done.
原文地址:http://blog.csdn.net/kingquake21/archive/2011/01/24/6160952.aspx
分享到:
相关推荐
NULL 博文链接:https://coolfire9201207034819.iteye.com/blog/1660744
java中yieldsleep以及wait的区别.pdf
sleep()与wait()的区别 1.这两个方法来自不同的类,sleep是Thread类的方法,而wait是Object类的方法; 2.执行sleep方法后不会释放锁,而执行wait方法后会释放锁; 3.wait,notify和notifyAll只能在同步方法或同步...
T03_Sleep_Yield_Join.java
Java线程中wait、await、sleep、yield、join用法汇总,文章里面总结了这些关键字的用法,并且里面带有源码帮助分析用法,此一文就可以理解这些关键字用法,推荐拥有
CSDN前辈众多,本文要说全部原创,那不现实,但本文绝对走肾之文,概念知识多来自前辈博文讲解,特此感谢。 文章末尾会给出本人学习过程中翻阅的优秀博文。 正文 给出一段准备代码,相信各位一看就懂,不懂,你不懂...
高薪程序员面试题精讲系列63之说说sleep()、yield()、join()、wait()的区别.pdf,这是一份不错的文件
线程的基本概念、线程类、任务类、线程优先级、sleep()方法(休眠)、yield()方法(礼让)、join方法(合并)、interrupt()方法(中断),线程的生命周期 线程 与 进程 的关系:**有一个进程中至少包含一个线程 **...
之前,我讨论了一个wait()和sleep()方法区别的问题,这一次,我将会讨论join()和yield()方法的区别。坦白的说,实际上我并没有用过其中任何一个方法,所以,如果你感觉有不恰当的地方,请提出讨论。 Java线程调度...
主要介绍了Java sleep方法及中断方式、yield方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
长沙欧柏泰克软件学院专业老师精心为其学员整理的50个安卓面试习题及答案哦!学习安卓的童鞋们可以参考看看
Java中的线程让步会让线程让出优先级,而休眠则会让线程进入阻塞状态等待被唤醒,这里我们对比线程等待的wait()方法,来详解Java中的线程让步yield()与线程休眠sleep()方法
易语言协同程序接口模块源码,协同程序接口模块,子程序1,yield_sleep,coroutine_run,coroutine_init,init_check,coroutine_create,coroutine_callback,coroutine_destroy,coroutine_resume,coroutine_status,...
这是可以从过程循环中产生的特殊对象内核支持生成器*run() {}和普通的run() {}进程循环函数可调整的调度程序和安全的内存管理(每个进程都有自己的隔离内存)已实现的SYSCALL: SYSCALL 描述yield new Yield();...
sleep方法和wait方法的区别 stop,suspend,resume等方法为什么会被遗弃 interrupt,interrupted,isInterrupted方法区别 join方法 yield方法 多线程 进程和线程 进程与线程最主要的区别是它们是操作系统管理资源的不同...
在上述例子中,我们在main主线程中创建了两个新的线程lefthand和righthand。当lefthand调用start()开始运行时,类Lefthand中的run()将自动被执行。 我们来分析一下上面程序的输出结果。Left线程首先开始执行,这时...
sleep(1000); digitalWrite(13, LOW); sleep(1000); //多线程的loop } void setup() { mySCoop.start(); }//形式setup void loop() { yield(); }//形式loop 格式二 #include <SCoop.h>//引入头文件 ...
angular.js 的基于生成器的最终流控制优点和 为来自 angular.js 的生成器提供浏览器支持确保包含 runtime.js 非常基本的示例(borwserify / require.js): angular.module('test', []).controller('CtrlTest', ...
task.sleep(ms) Function.task(thisObj, args...) 例子 var fs = require ( 'fs' ) ; var task = require ( 'node-taskjs' ) ; function * test_func ( ) { console . log ( 'test' ) ; yield * task . sleep ...
虽然 SCoop 允许您编写看起来非常普通的代码,并通过yield和sleep调用来分散它,但 Tasker 要求您完全避免使用延迟。 值得庆幸的是,Tasker 通过提供灵活的调度机制(包括伪睡眠功能)和一种存储任务状态的简单...