Java并发常用方法 sleep 和 wait

一:sleep 和 wait

sleep()方法:

  1. 功能:让当前线程休眠指定时间,休眠时间的准确性依赖于系统时钟和CPU调度机制
  2. 是Thread类的静态方法
  3. 可在任何地方调用,需要处理InterruptedException
  4. 当前线程调用sleep()方法,当前线程会进入阻塞状态,sleep()结束进入可执行状态
  5. sleep()方法不释放已获取的锁资源

wait()方法:

  1. 让当前线程进入等待状态,当别的其他线程调用notify()或者notifyAll()方法时,当前线程进入可运行状态
  2. 是Object类的成员方法
  3. wait方法必须在同步上下文中调用,例如:同步方法块或者同步方法中,这也就意味着如果你想要调用wait方法,前提是必须获取对象上的锁资源
  4. 当wait方法调用时,当前线程将会释放已获取的对象锁资源,并进入等待队列,其他线程就可以尝试获取对象上的锁资源

sleep() vs wait()

sleepwait
同步不需要在同步方法或同步块中调用只能在同步上下文中调用wait方法,否则或抛出IllegalMonitorStateException异常
作用对象定义在java.lang.Thread中,作用于当前线程定义在Object类中,作用于对象本身
释放锁资源
唤醒条件超时或者调用interrupt()方法其他线程调用对象的notify()或者notifyAll()方法
方法属性sleep是静态方法wait是实例方法

二:方法签名:

sleep()方法是Thread类的静态方法

/*** 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;

wait()方法是Object类的成员方法

/*** Causes the current thread to wait until another thread invokes the* {@link java.lang.Object#notify()} method or the* {@link java.lang.Object#notifyAll()} method for this object.* In other words, this method behaves exactly as if it simply* performs the call {@code wait(0)}.* <p>* The current thread must own this object's monitor. The thread* releases ownership of this monitor and waits until another thread* notifies threads waiting on this object's monitor to wake up* either through a call to the {@code notify} method or the* {@code notifyAll} method. The thread then waits until it can* re-obtain ownership of the monitor and resumes execution.* <p>* As in the one argument version, interrupts and spurious wakeups are* possible, and this method should always be used in a loop:* <pre>*     synchronized (obj) {*         while (&lt;condition does not hold&gt;)*             obj.wait();*         ... // Perform action appropriate to condition*     }* </pre>* This method should only be called by a thread that is the owner* of this object's monitor. See the {@code notify} method for a* description of the ways in which a thread can become the owner of* a monitor.** @throws  IllegalMonitorStateException  if the current thread is not*               the owner of the object's monitor.* @throws  InterruptedException if any thread interrupted the*             current thread before or while the current thread*             was waiting for a notification.  The <i>interrupted*             status</i> of the current thread is cleared when*             this exception is thrown.* @see        java.lang.Object#notify()* @see        java.lang.Object#notifyAll()*/public final void wait() throws InterruptedException {wait(0);}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/313820.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

.NET Core 3.0之深入源码理解Host(二)

写在前面停了近一个月的技术博客&#xff0c;随着正式脱离996的魔窟&#xff0c;接下来也正式恢复了。本文从源码角度进一步讨论.NET Core 3.0 中关于Host扩展的一些技术点&#xff0c;主要内容是关于创建Long Run Program的创建与守护。关于Host&#xff0c;我们最容易想到的就…

Finding Hotels(牛客国庆集训派对Day7 )(2016ICPC青岛K)(K-D Tree)

Finding Hotels 给定二维平面上nnn个点&#xff0c;每个点描述为x,y,cx, y, cx,y,c&#xff0c;x,yx, yx,y为坐标&#xff0c;ccc为该点的价值&#xff0c; 有mmm个询问&#xff0c;每次询问给x,y,cx, y, cx,y,c&#xff0c;要求&#xff0c;点的价值小于等于ccc的条件下&…

Java多线程常用方法 wait 和 notify

一&#xff1a;从一道面试题说起 启动两个线程, 一个输出 1,3,5,7…99, 另一个输出 2,4,6,8…100 最后 STDOUT 中按序输出 1,2,3,4,5…100 要求用 Java 的 wait notify 机制来实现 解法&#xff1a; public class Test {private static Object lock new Object();private st…

dotNET Core实现分布式环境下的流水号唯一

业务背景在管理系统中&#xff0c;很多功能模块都会涉及到各种类型的编号&#xff0c;例如&#xff1a;流程编号、订单号、合同编号等等。编号各有各自的规则&#xff0c;但通常有一个流水号来确定编号的唯一性&#xff0c;保证流水号的唯一&#xff0c;在不同的环境中实现方式…

单位根反演小记

单位根反演 一个等式&#xff1a;[n∣a]1n∑k0n−1wnak[n \mid a] \frac{1}{n} \sum\limits_{k 0} ^{n - 1}w_n ^{ak}[n∣a]n1​k0∑n−1​wnak​ 证明&#xff1a; wnaw_n ^ awna​是nnn次单位根的aaa次方&#xff0c;所以这里是一个公比为wnaw_n ^ awna​的等比数列&…

.NET Core3发布Json API

我们给DNC3&#xff08;.NET Core 3&#xff09;上了一个新包&#xff0c;叫做System.Text.Json(点我下载)&#xff0c;支持读写器&#xff0c;DOM&#xff08;文档对象模型&#xff09;&#xff0c;和序列化&#xff0c;在这篇博文里&#xff0c;我会告诉大家为什么要做这个&a…

Java ThreadLocal

** 一&#xff1a;ThreadLocal的简要介绍及使用 ** Java中的ThreadLocal类允许我们创建只能被同一个线程读写的变量。因此&#xff0c;如果一段代码含有一个ThreadLocal变量的引用&#xff0c;即使两个线程同时执行这段代码&#xff0c;它们也无法访问到对方的ThreadLocal变…

P5591 小猪佩奇学数学(单位根反演)

P5591 小猪佩奇学数学 ∑i0n(in)pi⌊ik⌋⌊ik⌋i−i%kk1k∑i0n(in)pi(i−i%k)1k∑i0n(in)pii−1k∑i0n(in)pi(imodk)\sum_{i 0} ^{n} (_i ^ n) \times p ^ i \times \lfloor \frac{i}{k} \rfloor\\ \lfloor \frac{i}{k} \rfloor \frac{i - i \% k}{k}\\ \frac{1}{k} \sum_{i …

认证方案之初步认识JWT

前言&#xff1a;现在越来越多的项目或多或少会用到JWT&#xff0c;为什么会出现使用JWT这样的场景的呢&#xff1f;假设现在有一个APP&#xff0c;后台是分布式系统。APP的首页模块部署在上海机房的服务器上&#xff0c;子页面模块部署在深圳机房的服务器上。此时你从首页登录…

Java实现生产消费模型的5种方式

** 前言 ** 生产者和消费者问题是线程模型中的经典问题&#xff1a;生产者和消费者在同一时间段内共用同一个存储空间&#xff0c;生产者往存储空间中添加产品&#xff0c;消费者从存储空间中取走产品&#xff0c;当存储空间为空时&#xff0c;消费者阻塞&#xff0c;当存储…

C#.NET 一颗璀璨的全能明星

C# 是微软推出的一种基于.NET框架的、面向对象的高级编程语言&#xff0c;她可以做什么呢&#xff1f;1.桌面开发&#xff0c;WinForm/GUI可视化编程&#xff1a;Windows开发中的葵花宝典&#xff0c;霸主地位至今无出其右&#xff0c;开发效率令人发指&#xff0c;大部分营销软…

#3328. PYXFIB(单位根反演)

#3328. PYXFIB ∑i0⌊nk⌋CnikFik∑i0nCniFi[i≡0(modk)]i≡0(modk)&#xff0c;单位根反演有1k∑j0k−1wkij1k∑i0nCniFi∑j0k−1wkij\sum_{i 0} ^{\lfloor \frac{n}{k} \rfloor} C_{n} ^{i \times k} \times F_{i \times k}\\ \sum_{i 0} ^{n} C_n ^{i} \times F_i \times …

Leetcode 86. 分隔链表

给定一个链表和一个特定值 x&#xff0c;对链表进行分隔&#xff0c;使得所有小于 x 的节点都在大于或等于 x 的节点之前。你应当保留两个分区中每个节点的初始相对位置。示例:输入: head 1->4->3->2->5->2, x 3输出: 1->2->2->4->3->5题目分析…

深入理解 JVM Class文件格式(一)

** 一、JVM体系结构 ** ** 二、class格式文件概述 ** class文件是一种8位字节的二进制流文件&#xff0c; 各个数据项按顺序紧密的从前向后排列&#xff0c; 相邻的项之间没有间隙&#xff0c; 这样可以使得class文件非常紧凑&#xff0c; 体积轻巧&#xff0c; 可以被J…

min_25 推导及例题总结

min_25 筛 一个亚线性筛&#xff0c;复杂度大概是O(n34log⁡n)O(\frac{n ^{\frac{3}{4}}}{ \log n})O(lognn43​​)。 使用min_25min\_25min_25求前缀和&#xff0c;有两个基本特征&#xff1a;① 积性函数&#xff0c;② 满足质数点为多项式。 算法思路 给定n≤1011n \leq…

asp.net core 使用 signalR(一)

asp.net core 使用 signalR&#xff08;一&#xff09;IntroSignalR 是什么&#xff1f;ASP.NET Core SignalR 是一个开源代码库&#xff0c;它简化了向应用添加实时 Web 功能的过程。实时 Web 功能使服务器端代码能够即时将内容推送到客户端。SignalR 的适用对象&#xff1a;需…

深入理解 JVM Class文件格式(二)

** class文件中的特殊字符串 ** 特殊字符串是常量池中符号引用的一部分&#xff0c;包括三种&#xff1a; 类的全限定名&#xff0c; 字段和方法的描述符&#xff0c; 特殊方法的方法名。 下面我们就分别介绍这三种特殊字符串。 &#xff08;1&#xff09; 类的全限定名 在…

P4211 [LNOI2014]LCA(离线 + 在线 做法)

P4211 [LNOI2014]LCA 有一棵根节点为111树&#xff0c;有mmm次询问&#xff0c;每次给定l,r,zl, r, zl,r,z&#xff0c;输出∑ilrdep[lca(i,z)]\sum\limits_{i l} ^{r} dep[lca(i, z)]il∑r​dep[lca(i,z)]。 乍一看这题好像无从下手&#xff0c;仔细想想lca(i,z)lca(i, z)l…

.NET框架之“小马过河”

.NET框架之“小马过河”有许多流行的 .NET框架&#xff0c;大家都觉得挺“重”&#xff0c;认为很麻烦&#xff0c;重量级&#xff0c;不如其它“轻量级”框架&#xff0c;从而不愿意使用。面对形形色色的框架发愁&#xff0c;笔者也曾发愁。但我发现只要敢于尝试&#xff0c;这…

深入理解 JVM Class文件格式(三)

** JVM常量池中各数据项类型详解 ** 关于常量池的大概内容&#xff0c; 已经在 深入理解 JVM Class文件格式&#xff08;一&#xff09; 中讲解过了&#xff0c; 这篇文章中还介绍了常量池中的11种数据类型。 本文的任务是详细讲解这11种数据类型&#xff0c; 深度剖析源文件…