当前位置:网站首页>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 ?
边栏推荐
- HBCK fix problem
- PAT甲级1152 Google Recruitment (20 分)
- 谷歌云盘如何关联Google Colab
- Get the ask code corresponding to the key pressed by the keyboard
- The development summary of the function of fast playback of audio and video in any format on the web page.
- 分布式原理 - 什么是分布式系统
- 苹果内购和Apple Pay 的区别
- 二进制补码
- Spark AQE
- matlab randint,Matlab的randint函数用法「建议收藏」
猜你喜欢

Ml speech depth neural network model

《图书馆管理系统——“借书还书”模块》项目研发阶段性总结

Idea remotely submits spark tasks to the yarn cluster

ML - natural language processing - Basics

Take you to create your first C program (recommended Collection)

分布式原理 - 什么是分布式系统

matlab 如何保存所有运行后的数据

ML - 图像 - 深度学习和卷积神经网络

异步fifo的实现

Graph theory and concept
随机推荐
matlab randint,Matlab的randint函数用法「建议收藏」
Box avoiding mouse
The difference between Apple buy in and apple pay
Graph theory and concept
Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
wait()和sleep()的区别理解
Spark partition operators partitionby, coalesce, repartition
PAT甲级1152 Google Recruitment (20 分)
Node learning
Solve the timeout of dbeaver SQL client connection Phoenix query
Spark submission parameters -- use of files
JS URLEncode function
本地缓存--Ehcache
理解“平均负载”
Spark SQL common time functions
JVM知识脑图分享
C#精挑整理知识要点9 集合2(建议收藏)
Idea eye care settings
ML - 自然语言处理 - 自然语言处理简介
C # carefully sorting out key points of knowledge 11 entrustment and events (recommended Collection)