zoukankan      html  css  js  c++  java
  • Thread.sleep( ) vs Thread.yield( )


    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.)

    enter image description hereNote:

    • 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.
  • 相关阅读:
    go语言和c++比较
    追逐光和影,成就风与电
    vue中使用mockjs
    JS 实现关键字文本搜索 高亮显示
    vue将页面(dom元素)转换成图片
    js复制功能(pc复制,移动端复制到手机剪切板)
    Django终端打印SQL语句
    Oracle查看表空间及大小
    Oracle 五种约束的创建和移除:
    Oracle的decimal和number的对比
  • 原文地址:https://www.cnblogs.com/yunxiblog/p/5215670.html
Copyright © 2011-2022 走看看