Thread.sleep()

  1. The current thread changes state from Running to Waiting/Blocked as shown in the diagram below.
  2. Any other thread with reference to the thread currently sleeping (say t) can interrupt it calling t.interrupt()
    • the call to sleep has to be encapsulated to catch the checked exception of InterruptedException
  3. After the time period for which the thread was set to sleep it goes to the Runnable state andmight not run immediately! It has to wait for the Thread Scheduler to schedule it for its time slice.

Thread.yield()

  1. Calling it may cause the Thread Scheduler to move the current thread from Running toRunnable state and execute another same priority thread which was in Runnable state. This transition of state takes place only if there is some other thread of same priority in Runnable state. Hence the no guarantee that the thread will stop execution as the criteria of another same priority thread might not be met.
  2. .yield() is much based on the Thread Priorities concept. (All thread are assigned priorities and when a thread of higher priority is in Runnable state it ususally preempts / stops execution of lower priority threads depending on implementation of ThreadScheduler.)

Note:

  • both Thread.sleep() and Thread.yield() are static functions and affect the current thread executing it.
  • both the functions will not let go the synchronized locks they hold.

最新文章

  1. 解决Safari高版本浏览器中默认禁用第三方COOKIE(含demo)
  2. javascript类继承的一些实验
  3. 微软BI 之SSIS 系列 - 在 SSIS 中导入 ACCESS 数据库中的数据
  4. ylbtech-dbs:ylbtech-1,FAM(家庭资产管理系统)
  5. WT588D播放合成语音时出现某些语句不能正常播报的情况,经过对比其他语句,看似有点不符合逻辑。
  6. 【转】单例模式(python/c++)
  7. poj 3045 Cow Acrobats(二分搜索?)
  8. android screenOrientation
  9. C++转让Lua
  10. BZOJ 4539: [Hnoi2016]树 [主席树 lca]
  11. rem是怎么计算的(转载)
  12. Go 环境配置
  13. hibernate框架学习之二级缓存(测试用例)
  14. 再谈树形dp
  15. python-day72--django实现的cookie/session
  16. JavaScript -- Screen
  17. 自定义一个可以被序列化的泛型Dictionary<TKey,TValue>集合
  18. python3字符集之间--encode与decode之间的转码详解
  19. 使用Html.EditorFor()为文本框加上maxlength,placeholder等属性
  20. HDU 5154 Harry and Magical Computer 有向图判环

热门文章

  1. 使用HTTP代理
  2. visio去除直线交叉处的歪曲
  3. PHP 类与对象 全解析方法
  4. ArcGIS Data Interoperability 的使用(1)
  5. spring-cloud服务网关中的Timeout设置
  6. jeecms项目相关配置文件
  7. 清除样式的css
  8. [Day3] Nginx配置Https
  9. 洛谷P4244 [SHOI2008]仙人掌图 II
  10. FamilyFilter(4)