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的更多相关文章

  1. 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

  2. 记一次tomcat线程创建异常调优:unable to create new native thread

    测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...

  3. 多线程爬坑之路-Thread和Runable源码解析

    多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...

  4. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

    学习架构探险,从零开始写Java Web框架时,在学习到springAOP时遇到一个异常: "C:\Program Files\Java\jdk1.7.0_40\bin\java" ...

  5. Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V

    在学习CGlib动态代理时,遇到如下错误: Exception in thread "main" java.lang.NoSuchMethodError: org.objectwe ...

  6. Thread.Sleep(0) vs Sleep(1) vs Yeild

    本文将要提到的线程及其相关内容,均是指 Windows 操作系统中的线程,不涉及其它操作系统. 文章索引 核心概念 Thread.Yeild       Thread.Sleep(0) Thread. ...

  7. Android笔记——Handler Runnable与Thread的区别

    在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...

  8. Java Thread 的 sleep() 和 wait() 的区别

    Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别       1. sleep ...

  9. Android线程管理之Thread使用总结

    前言 最近在一直准备总结一下Android上的线程管理,今天先来总结一下Thread使用. 线程管理相关文章地址: Android线程管理之Thread使用总结 Android线程管理之Executo ...

  10. C# - 多线程 之 Process与Thread与ThreadPool

    Process 进程类, // 提供对本地和远程进程的访问,启动/停止本地系统进程 public class Process : Component { public int Id { get; } ...

随机推荐

  1. Python学习day36-并发编程(2)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  2. python 之 heapq (堆)

    堆的实现通过构造二叉堆,实为二叉树的一种:这种数据结构具有以下性质: 任意节点小于(或大于)它的后裔,最小元(或最大元)在堆的根上 堆总是一颗完整树.即除了最低层,其它层的节点都被元素填满,且最低层极 ...

  3. Diff- Linux必学的60个命令

    1.作用 diff命令用于两个文件之间的比较,并指出两者的不同,它的使用权限是所有用户. 2.格式 diff [options] 源文件 目标文件 3.[options]主要参数 -a:将所有文件当作 ...

  4. ROS多线程编程

    int main(int argc, char **argv) { ros::init(argc, argv, "multi_sub"); multiThreadListener ...

  5. 3926: [Zjoi2015]诸神眷顾的幻想乡

    传送门 一个广义后缀自动机模板. //Achen #include<algorithm> #include<iostream> #include<cstring> ...

  6. 90 k数和 II

    原题网址:https://www.lintcode.com/problem/k-sum-ii/description 描述 Given n unique integers, number k (1&l ...

  7. shiro+jwt+springboot理解

    转自 https://www.cnblogs.com/fengli9998/p/6676783.html https://www.jianshu.com/p/0366a1675bb6 https:// ...

  8. Simple implementation and results of genetic algorithm.

    This experiment was done for the final assignment of my Professional English class. This part has be ...

  9. Vertex Covers(高维前缀和)

    Vertex Covers 时间限制: 5 Sec  内存限制: 128 MB提交: 5  解决: 3 题目描述 In graph theory, a vertex cover of a graph ...

  10. 关于CE的反思

    当你注视着你的分数, 眼眶倏地猛睁. 不会做的题血红一片, 认真做了的题一点墨蓝. 你知道, 你CE了, 你挂了, 你倒数第一了, 你当场去世了. 两小时的努力付诸东流, 线段树的碎片历历在目. 思考 ...