当前位置:网站首页>Understanding the difference between wait() and sleep()
Understanding the difference between wait() and sleep()
2022-07-25 15:29:00 【Don't stop laughing】
Tell the truth , Except that their execution will make the thread blocked , I don't think they are comparable .
1.wait()、notify() and notifyAll() Methods are Object Methods , Can only be performed in critical areas .
2.wait() The lock identification will be released after execution , The usage scenario is that some threads that do not meet the conditions grab the lock , In order to make it execute after the condition is satisfied , Just call this method . Then wait until the conditions are met , Call again notify() and notifyAll() Let it live, they wake up .
3.wait() After giving up the lock sign , other Runnable The thread in state can execute normally , And be wait The thread of is only notify() and notifyAll() Before they are qualified to enter the lock grabbing army , That is to say, they were Runnable 了 .
4.wait() After entering the blocking state is with sleep() It's different after , be called “ Wait for blocking ”.JVM Will put them in the waiting queue . Of course, this waiting queue is different from that of the lock grabbing army . The lock grabbing army is in a queue full of Runnable Thread in state , Those who continue to sharpen their fists after failing to grab the lock , Their blocking state is called “ Synchronization blocking state ”. That is to say, out of “ Wait for blocking ” The thread of , After being awakened , If you can't grab the lock, it will become Runnable The state of tie Hanhan and others Runnable Tie Hanhan lives together “ Synchronization blocking state ”.
5.sleep() yes Thread Methods , Add time when using , And throw exceptions . It's troublesome , I don't like it a little . When it's called , The lock identification will not be released , Everyone is waiting for it sleep, It's selfish . Of course, threads are not so humanized , it sleep It is to let the thread that communicates priority with it execute first .
Here is a picture of Baidu's arrival , The explanation is very clear . Fabulous !

Finally notify() and notifyAll() Words , They are always mentioned above , It's a little unreasonable not to introduce .
1.notify() Randomly wake up a person in “ Wait for blocking ” The thread of .
2.notifyAll() As the name suggests, it is to wake up completely in “ Wait for blocking ” The thread of .
We feel , Only one thread can grab the lock , Then let's wake up whether it's just right ?
Remember that , It feels unreliable . Especially in the logic rigorous programming world , Unreliability means risk !
You wake up one randomly every time , That is to say , There is a chance that a thread will never be awakened ?
therefore ,notifyAll() Would it be more appropriate ?
边栏推荐
- The development summary of the function of fast playback of audio and video in any format on the web page.
- Qtime定义(手工废物利用简单好看)
- 获取键盘按下的键位对应ask码
- CF685B-求有根树每颗子树的重心
- Notes on inputview and inputaccessoryview of uitextfield
- Week303 of leetcode
- Box avoiding mouse
- 请问seata中mysql参数每个客户端连接最大的错误允许数量要怎么理解呢?
- ZOJ - 4114 Flipping Game-dp,合理状态表示
- C # carefully sorting out key points of knowledge 11 entrustment and events (recommended Collection)
猜你喜欢
随机推荐
Implementation of asynchronous FIFO
ML - natural language processing - Introduction to natural language processing
Singleton mode 3-- singleton mode
C # fine sorting knowledge points 9 Set 2 (recommended Collection)
GAMES101复习:线性代数
matlab 优化工具 manopt 安装
Idea remotely submits spark tasks to the yarn cluster
Simulate setinterval timer with setTimeout
CGO is realy Cool!
Local cache --ehcache
本地缓存--Ehcache
2019陕西省省赛J-位运算+贪心
从 join on 和 where 执行顺序认识T-sql查询执行顺序
ML - 自然语言处理 - 自然语言处理简介
Box avoiding mouse
Spark partition operators partitionby, coalesce, repartition
UITextField的inputView和inputAccessoryView注意点
redis淘汰策列
Yan required executor memory is above the max threshold (8192mb) of this cluster!
Find out what happened in the process of new









