当前位置:网站首页>Wait (), notify (), notifyAll (), sleep (), condition, await (), signal()
Wait (), notify (), notifyAll (), sleep (), condition, await (), signal()
2022-06-25 11:20:00 【User 9854323】
wait()、notify() and notifyAll() yes Object class The method in From the text description of these three methods, we can know the following information : 1) wait()、notify() and notifyAll() Methods are local methods , And for final Method , Can't be rewritten . 2) Call... Of an object wait() Method to block the current thread , And the current thread must own this object monitor( Immediate lock ) 3) Call... Of an object notify() Method can wake up a waiting object monitor The thread of , If there are multiple threads waiting for this object monitor, You can only wake up one of the threads ; 4) call notifyAll() Method can wake up all waiting for this object monitor The thread of ; 5 ) If you call the wait() Method , The current thread must have the monitor( Immediate lock ), So call wait() Method must be in synchronization block or synchronization method (synchronized Block or synchronized Method ). 6 ) Call... Of an object wait() Method , Equivalent to having the current thread hand over the object monitor, Then enter the waiting state , Wait for the lock of this object to be acquired again later (Thread Class sleep The thread pauses the execution of the method for a period of time , This gives other threads a chance to continue , But it doesn't release the object lock ); 7 ) Call... Of an object notify() Method , The current thread must also own this object monitor, So call notify() Method must be in synchronization block or synchronization method (synchronized Block or synchronized Method ).
Condition Is in java 1.5 It's not until now , It's used to replace the traditional Object Of wait()、notify() Realize the cooperation between threads , Compared with Object Of wait()、notify(), Use Condition Of await()、signal() It is safer and more efficient to realize the cooperation between threads in this way . Therefore, it is generally recommended to use Condition.
1 ) Condition It's an interface , The basic method is await() and signal() Method ; 2 ) Condition Depend on Lock Interface , Generate a Condition The basic code is lock.newCondition() 3 ) call Condition Of await() and signal() Method , Must be in lock Within protection , That is to say, it must be lock.lock() and lock.unlock Can only be used between
Conditon Medium await() Corresponding Object Of wait(); Condition Medium signal() Corresponding Object Of notify(); Condition Medium signalAll() Corresponding Object Of notifyAll()
边栏推荐
- A program reflecting the characteristics of C language program structure
- relu与sigmod的比较
- 如何实现移动端富文本编辑器功能
- Android: generic mapping analysis of gson and JSON in kotlin
- 数据库系列:MySQL索引优化总结(综合版)
- Shen Ying, China Academy of communications and communications: font open source protocol -- Introduction to ofl v1.1 and analysis of key points of compliance
- Kingbasees plug-in DBMS of Jincang database_ RANDOM
- Démarrer avec Apache shenyu
- Daily 3 questions (2) - find out the lucky numbers in the array
- SQL注入漏洞(繞過篇)
猜你喜欢
随机推荐
MySQL synchronous data configuration and shell script implementation
Jincang database kingbasees plug-in force_ view
Ouverture de l'inscription | le troisième marathon des hackers de pagaie est arrivé comme prévu.
SystemVerilog (XIII) - enumerate data types
SQL注入漏洞(类型篇)
3 Questions par jour (3) - vérifier l'existence d'entiers et de leurs doubles
Use of three-level linkage plug-ins selected by provinces and cities
Handler、Message、Looper、MessageQueue
FPGA基于VGA显示字符及图片
金仓数据库 KingbaseES 插件force_view
基于C语言的图书信息管理系统 课程论文+代码及可执行exe文件
ZABBIX distributed system monitoring
Network remote access using raspberry pie
Upload and modify the use of avatars
【文件包含漏洞-04】经典面试题:已知某网站仅存在本地文件包含漏洞时,如何GetShell?
C disk uses 100% cleaning method
龙书虎书鲸书啃不动?试试豆瓣评分9.5的猴书
Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
Arrays. asList()
Writing wechat applet with uni app