点击上方 "程序员小乐"关注, 星标或置顶一起成长

每天凌晨00点00分, 第一时间与你相约

每日英文

Good friends are like stars. You don’t always see them, but you know they’re always there.

好朋友就像是星星。 你不一定总是能见到他们,但你知道,他们会一直在那里。

每日掏心

有时候,明明心如刀割,却要灿烂的微笑,明明很脆弱,却表现的如此坚强,眼泪在眼里打转,却告诉每个人我很好。

来自:芊烛 | 责编:乐乐

链接:cnblogs.com/yonim/p/11417809.html

程序员小乐(ID:study_tech)第 722 次推文   图片来自网络

往日回顾:买到春节回家的票了吗?用Python刷票,还不快来试试!(建议收藏) 为回家做准备

   正文   

好了、说了多线程,那就不得不说说多线程的sleep()、join()和yield()三个方法的区别啦

1、sleep()方法

/*** Causes the currently executing thread to sleep (temporarily cease* execution) for the specified number of milliseconds, subject to* the precision and accuracy of system timers and schedulers. The thread* does not lose ownership of any monitors.*  意思是说:当前正在执行的线程休眠(暂时停止执行)指定的毫秒数,具体取决于系统计时器和调度程序的精度和准确性。该线程不会失去任何监视器的所有权。* @param  millis*         the length of time to sleep in milliseconds  *         毫秒为单位* @throws  IllegalArgumentException*          if the value of {@code millis} is negative** @throws  InterruptedException*          if any thread has interrupted the current thread. The*          <i>interrupted status</i> of the current thread is*          cleared when this exception is thrown.*/public static native void sleep(long millis) throws InterruptedException;

其实主要的就是他是让其他线程走,自己进行休眠,但是自己却不会释放对象锁,也就是说,如果有同步锁的时候,其他线程不能访问共享数据。

注意该方法要捕获异常 比如有两个线程同时执行(没有Synchronized),一个线程优先级为MAX_PRIORITY,另一 个为MIN_PRIORITY,如果没有Sleep()方法,只有高优先级的线程执行完成后,低优先级 的线程才能执行;但当高优先级的线程sleep(5000)后,低优先级就有机会执行了。总之,sleep()可以使低优先级的线程得到执行的机会,当然也可以让同优先级、高优先级的 线程有执行的机会。

2、yield() 方法

/*** A hint to the scheduler that the current thread is willing to yield* its current use of a processor. The scheduler is free to ignore this* hint.*   意思是说 提示当前线程可以让处理器忽略当前线程,去处理其他线程* <p> Yield is a heuristic attempt to improve relative progression* between threads that would otherwise over-utilise a CPU. Its use* should be combined with detailed profiling and benchmarking to* ensure that it actually has the desired effect.* 它是一种启发式尝试,用于改善线程之间的相对进展,否则会过度利用CPU。它的使用应与详细的分析和基准测试相结合,以确保它实际上具有所需的效果。* <p> It is rarely appropriate to use this method. It may be useful* for debugging or testing purposes, where it may help to reproduce* bugs due to race conditions. It may also be useful when designing* concurrency control constructs such as the ones in the* {@link java.util.concurrent.locks} package.*  使用这种方法很少是合适的。它可能对调试或测试目的很有用,它可能有助于重现因竞争条件而产生的错误。在设计并发控制结构(如中的那些)时,它也可能很有用*/public static native void yield();

yield() 这个方法从以上注释可以看出,也是一个休眠自身线程的方法,同样不会释放自身锁的标识,区别在于它是没有参数的,即yield()方 法只是使当前线程重新回到可执行状态,所以执行yield()的线程有可能在进入到可执行状态 后马上又被执行,另外yield()方法只能使同优先级或者高优先级的线程得到执行机会,这也 和sleep()方法不同。

3、join() 方法

这个方法比较有意思,Thread的非静态方法join()让一个线程B“加入”到另外一个线程A的尾部。在A执行完毕之前, B不能工作。

/*** Waits for this thread to die.  *  等待线程死亡* <p> An invocation of this method behaves in exactly the same* way as the invocation** <blockquote>* {@linkplain #join(long) join}{@code (0)}* </blockquote>** @throws  InterruptedException*          if any thread has interrupted the current thread. The*          <i>interrupted status</i> of the current thread is*          cleared when this exception is thrown.*/public final void join() throws InterruptedException {join(0);  // 调用了有参方法}
/*** Waits at most {@code millis} milliseconds for this thread to* die. A timeout of {@code 0} means to wait forever.*  这个线程最多等多少毫秒,如果超时了,就会进行线程死锁* <p> This implementation uses a loop of {@code this.wait} calls* conditioned on {@code this.isAlive}. As a thread terminates the* {@code this.notifyAll} method is invoked. It is recommended that* applications not use {@code wait}, {@code notify}, or* {@code notifyAll} on {@code Thread} instances.** @param  millis*         the time to wait in milliseconds** @throws  IllegalArgumentException*          if the value of {@code millis} is negative** @throws  InterruptedException*          if any thread has interrupted the current thread. The*          <i>interrupted status</i> of the current thread is*          cleared when this exception is thrown.*/public final synchronized void join(long millis)throws InterruptedException {

保证当前线程停止执行,直到该线程所加入的线程完成为止。然而,如果它加入的线程没有存活,则当前线程不需要停止。

欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,学习能力的提升上有新的认识,欢迎转发分享给更多人。

欢迎各位读者加入程序员小乐技术群,在公众号后台回复“加群”或者“学习”即可。

猜你还想看

阿里、腾讯、百度、华为、京东最新面试题汇集

探究 Nginx 中 reload 流程的真相

不了解Java反射机制?看这篇就明白了!

HTTPS 原理分析:带着疑问层层深入

关注「程序员小乐」,收看更多精彩内容

嘿,你在看吗

关于多线程中sleep、join、yield的区别相关推荐

  1. java多线程中的join方法详解

    java多线程中的join方法详解 方法Join是干啥用的? 简单回答,同步,如何同步? 怎么实现的? 下面将逐个回答. 自从接触Java多线程,一直对Join理解不了.JDK是这样说的:join p ...

  2. Python 多线程中的 join() 和 setDaemon()

    Demo 是最好的老师!!! 参考链接:Python多线程与多线程中join()的用法 - cnkai - 博客园 知识点一(setDaemon(False)): 当一个进程启动之后,会默认产生一个主 ...

  3. 你能说出多线程中 sleep、yield、join 的用法及 sleep与wait区别吗?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 作者:noteless cnblogs.com/noteless/ ...

  4. Java多线程中Sleep与Wait的区别

    Java中的多线程是一种抢占式的机制,而不是分时机制.抢占式的机制是有多个线程处于可运行状态,但是只有一个线程在运行. 共同点: 1. 他们都是在多线程的环境下,都可以在程序的调用处阻塞指定的毫秒数, ...

  5. left join on or 优化_pandas中merge/join有什么区别?

    越来越多的人学习python,更确切的说是pandas,包中最让人迷惑的是merge 和join,以下将说一说他们之间的区别和联系: 总的来说:pandas.merge()才是老大,merge/joi ...

  6. python 多线程中的 join 和 daemon

    文章目录 第一关:简单的 join() 第二关:join(timeout) 第三关:setDaemon(True) 第一关:简单的 join() import threading import tim ...

  7. java join使用实例_Java多线程中关于join方法的使用实例解析

    先上代码 新建一个Thread,代码如下: package com.thread.test; public class MyThread extends Thread { private String ...

  8. Java多线程中run和start的区别

    Thread的start和run 1) start: 用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码.通过调用Thread类的start ...

  9. 多线程中,NSOperationQueue和GCD的区别

    1.效率肯定是delegate比nsnotification高. 2. delegate方法比notification更加直接,最典型的特征是,delegate方法往往需要关注返回值, 也就是dele ...

  10. Python多线程中阻塞(join)与锁(Lock)的使用误区

    参考资料:https://blog.csdn.net/cd_xuyue/article/details/52052893 1使用两个循环分别处理start和join函数.即可实现并发. threads ...

最新文章

  1. Color Picker like PhotoShop
  2. C++的this指针和引用符号的搭配使用理解
  3. unix网络编程 ubuntu下搭建环境编译源码
  4. iOS开发点击UIButton实现UIView的旋转
  5. VHDL数字秒表的设计
  6. vc设备工程师_4注册公用设备工程师专业基础考试真题.
  7. java activiti jbpm_activiti和jbpm工作流引擎哪个比较好?
  8. 《深入实践Spring Boot》下载
  9. jboss fuse 教程_JBoss Fuse –使用MVEL将您的静态配置转换为动态模板
  10. 七大排序的个人总结(二) 归并排序(Merge
  11. numpy维度交换_如何将2个不同维度的numpy数组相乘
  12. Easytrader踩坑之旅(一)
  13. 小程序的网络请求封装
  14. 概率论 方差公式_概率论复习:重要概念和公式
  15. C语言经典实例003:输出名言
  16. 【C语言】简单的飞机游戏
  17. 垃圾网线,毁我青春(ubuntu安装失败)
  18. 种子的“选择”可影响地区生物多样性
  19. 改名叫Benson...
  20. NLP聊天机器人的搭建(chatbot)(一)

热门文章

  1. 山东理工大学首页html
  2. (转)负载大逃亡:四十二路怪兽联军及七条逃生法则
  3. [附源码]JSP+ssm计算机毕业设计小学生作业帮平台的设计与实现0in3j【源码、数据库、LW、部署】
  4. 树莓派html编辑器,BlocklyPi
  5. 20110805 组队赛 f题
  6. android 服务器sessionid,Android用WebView获取sessionid保持登录状态
  7. 文远知行杯广东工业大学第十六届程序设计竞赛错题笔记
  8. 工具:帆软FineReport高级使用指南(二)
  9. 数据库设计规范、E-R图、模型图
  10. vivo手机录屏怎么弄?小技巧快来掌握