Java - Difference between Wait and Sleep , Yield in Java
2015-10-08 15:49
543 查看
http://javarevisited.blogspot.de/2011/12/difference-between-wait-sleep-yield.html
Because sometimes I can't open this page without proper proxy configuration, so I have to copy some content here.
Difference between Wait and Sleep in Java
Main difference between wait and sleep is that wait() method release the acquired monitor while Thread.sleep() method keeps the lock or monitor.
Also wait method in java should be called from synchronized method or block while there is no such requirement for sleep() method.
Another difference is Thread.sleep() method is a static method and applies on current thread, while wait() is an instance specific method and only got wake up if some other thread calls notify method on same object.
Also in case of sleep, sleeping thread immediately goes to Runnable state after waking up while in case of wait, waiting thread first acquires the lock and then goes into Runnable state.
So based upon your need if you require a specified second of pause use sleep() method or if you want to implement inter-thread communication use wait method.
Here is list of difference between wait and sleep in Java :
1) wait is called from synchronized context only, while sleep can be called without synchronized block.
2) wait is called on Object, while sleep is called on Thread.
3) waiting thread can be awake by calling notify and notifyAll, while sleeping thread can not be awaken by calling notify method.
4) wait is normally done on condition. Thread wait until a condition is true, while sleep is just to put your thread on sleep.
5) wait release lock on object while waiting, while sleep doesn’t release lock while waiting.
Difference between yield and sleep in java
Both sleep() and yield() operate on current thread.
Major difference between yield and sleep in Java is that yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting
thread or all the waiting threads have a lower priority, then the current thread will continue its execution. When the yielded thread will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent. Yield method doesn’t
guarantee that current thread will pause or stop but it guarantee that CPU will be relinquished by current Thread when Thread.yield() method is called in java.
10 points about Thread sleep() method in Java
1) Thread.sleep() method is used to pause the execution, relinquish the CPU and return it to thread scheduler.
2) Thread.sleep() method is a static method and always puts current thread on sleep.
3) Java has two variants of sleep method in Thread class. one has one argument which takes milliseconds as duration for sleep, other method has two arguments: one is millisecond and other is nanosecond.
4) Unlike wait() method in Java, sleep() method of Thread class doesn't relinquish the lock that it has acquired.
5) sleep() method throws Interrupted Exception if another thread interrupt a sleeping thread in java.
6) sleep() method in Java does not guaranteed that woke up thread will definitely get CPU, instead it will go to Runnable state and fight for CPU with other thread.
7) There is a misconception about sleep method in Java that calling t.sleep() will put Thread "t" into sleeping state, that's not true. Because Thread.sleep method is a static method, it always put current thread into Sleeping state and not thread "t".
Because sometimes I can't open this page without proper proxy configuration, so I have to copy some content here.
Difference between Wait and Sleep in Java
Main difference between wait and sleep is that wait() method release the acquired monitor while Thread.sleep() method keeps the lock or monitor.
Also wait method in java should be called from synchronized method or block while there is no such requirement for sleep() method.
Another difference is Thread.sleep() method is a static method and applies on current thread, while wait() is an instance specific method and only got wake up if some other thread calls notify method on same object.
Also in case of sleep, sleeping thread immediately goes to Runnable state after waking up while in case of wait, waiting thread first acquires the lock and then goes into Runnable state.
So based upon your need if you require a specified second of pause use sleep() method or if you want to implement inter-thread communication use wait method.
Here is list of difference between wait and sleep in Java :
1) wait is called from synchronized context only, while sleep can be called without synchronized block.
2) wait is called on Object, while sleep is called on Thread.
3) waiting thread can be awake by calling notify and notifyAll, while sleeping thread can not be awaken by calling notify method.
4) wait is normally done on condition. Thread wait until a condition is true, while sleep is just to put your thread on sleep.
5) wait release lock on object while waiting, while sleep doesn’t release lock while waiting.
Difference between yield and sleep in java
Both sleep() and yield() operate on current thread.
Major difference between yield and sleep in Java is that yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting
thread or all the waiting threads have a lower priority, then the current thread will continue its execution. When the yielded thread will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent. Yield method doesn’t
guarantee that current thread will pause or stop but it guarantee that CPU will be relinquished by current Thread when Thread.yield() method is called in java.
10 points about Thread sleep() method in Java
1) Thread.sleep() method is used to pause the execution, relinquish the CPU and return it to thread scheduler.
2) Thread.sleep() method is a static method and always puts current thread on sleep.
3) Java has two variants of sleep method in Thread class. one has one argument which takes milliseconds as duration for sleep, other method has two arguments: one is millisecond and other is nanosecond.
4) Unlike wait() method in Java, sleep() method of Thread class doesn't relinquish the lock that it has acquired.
5) sleep() method throws Interrupted Exception if another thread interrupt a sleeping thread in java.
6) sleep() method in Java does not guaranteed that woke up thread will definitely get CPU, instead it will go to Runnable state and fight for CPU with other thread.
7) There is a misconception about sleep method in Java that calling t.sleep() will put Thread "t" into sleeping state, that's not true. Because Thread.sleep method is a static method, it always put current thread into Sleeping state and not thread "t".
相关文章推荐
- 在实现文本框只能输入数字和小数点的基础上实现了价格样式(保留两位小数)
- img及父元素(容器)实现类似css3中的background-size:contain / background-size:cover
- angularjs input上传图片前获取图片的Size
- 【笔记】利用CSS3特性实现圆角DIV
- Javascript DOM
- setTimeout,setInterval,process.nextTick,setImmediate in Nodejs
- VM11 vmware-tools 共享文件夹 报错
- bootstrap中fuelux.tree的简单使用
- JS获取浏览器型号
- C#中js无效,不弹出提示语句,与ScriptManager冲突导致
- 经典收藏 50个jQuery Mobile开发技巧集萃
- HTML
- nodejs中exports与module.exports的实践
- jQuery Mobile案例,最近用Moon.Web和Moon.Orm做了一套系统
- javascript,一道容易犯错的小题
- button跳转jsp
- javascript,clone对象
- JS实现时钟程序
- Jsonkit中isa错误解决办法
- CoffeeScript之,存在操作符“?”