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.

Thread.sleep( ) vs Thread.yield( )的更多相关文章

  1. Thread系列之Thread.Sleep(0)

    线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...

  2. PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别

    链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...

  3. Part 92 Significance of Thread Join and Thread IsAlive functions

    Thread.Join & Thread.IsAlive functions Join blocks the current thread and makes it wait until th ...

  4. Thread thread2 = new Thread()

    Thread thread2 = new Thread() { @Override public void run() { test.function(); } }; thread1.start(); ...

  5. Mysql thread 与 OS thread

    测试环境信息如下: OS:Ubuntu 16.04 LTS Mysql:Mysql 5.7.18,使用docker images运行的实例 Mysql如何处理client请求 在Mysql中,连接管理 ...

  6. c++并发编程之thread::join()和thread::detach()

    thread::join(): 阻塞当前线程,直至 *this 所标识的线程完成其执行.*this 所标识的线程的完成同步于从 join() 的成功返回. 该方法简单暴力,主线程等待子进程期间什么都不 ...

  7. yum安装提示错误Thread/process failed: Thread died in Berkeley DB library

    问题描述: yum 安装更新提示 rpmdb: Thread/process failed: Thread died in Berkeley DB library 问题解决: 01.删除yum临时库文 ...

  8. 1,Thread 概念以及Thread 的6个状态

    Thread 有6个状态 , NEW, RUNNABLE , BLOCKED, WATTING, TIMED WAITING, TERMINATED 1.NEW至今尚未启动的线程的状态.2.RUNNA ...

  9. Thread之三:Thread Join()的用法

    一.join用法 join()和wait()不会释放锁,join()是Thread的方法,wait()是Object的方法 1.join方法定义在Thread类中,则调用者必须是一个线程 例如: Th ...

随机推荐

  1. Python - 基本数据类型及其常用的方法之数字与字符串

    数字(int): 1.int()(将字符串换为数字) a = " print(type(a), a) b = int(a) print(type(b), b) num = "a&q ...

  2. C开发系列-函数

    概述 任何一个C语言程序都是有一个或多个程序段(小程序构成).每个程序都有自己的功能,我们一般称这些程序段为"函数". 函数的执行过程 #import <Foundation ...

  3. 如何将Map键值的下划线转为驼峰

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:如何将Map键值的下划线转为驼峰: 例,将HashMap实例extMap键值下划线转为驼峰: 代码: HashMap<String ...

  4. win10 安装face_recongnition

    1.安装dlib https://stackoverflow.com/questions/41912372/dlib-installation-on-windows-10 2.安装face_recon ...

  5. Python开发利器Pycharm

    可以新建项目的时候,在下面的小扳手那里创建虚拟化环境.若想安装包,直接file->setting里面找到对应项目,然后添加新的包即可.   视频地址:http://edu.51cto.com/c ...

  6. PageHelper原理

    1.要是用pagehelper,首先maven项目,要引入 <dependency> <groupId>com.github.pagehelper</groupId> ...

  7. c++ 读取8, 10, 16进制数

    c++基础知识都快忘了..记一下 dec-十进制(默认) oct-八进制 hex-十六进制

  8. PAT甲级——A1046 Shortest Distance

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  9. Life of Pi

    ·when you look into his eyes,you are seeing your own emotionsreflected back at you,nothing else. ·Go ...

  10. 转载:JVM内存分代策略

    Java虚拟机根据对象存活的周期不同,把堆内存划分为几块,一般分为新生代.老年代和永久代(对HotSpot虚拟机而言),这就是JVM的内存分代策略. 为什么要分代? 堆内存是虚拟机管理的内存中最大的一 ...