当前位置:网站首页>[concurrent programming] collaboration between threads
[concurrent programming] collaboration between threads
2022-07-03 08:37:00 【keeper42】
2022.02.09
Collaboration between threads
polling : It is difficult to guarantee timeliness , Resources are expensive ,
Waiting and notifying
wait() Method on object
notify/notifyAll Method on object
The standard paradigm for waiting and notification
Waiting party :
- Get the lock of the object ;
- In the cycle, judge whether the conditions are satisfied , Not satisfied with call wait Method ,
- Conditions meet to execute business logic
The informer said
- Get the lock of the object ;
- Change the conditions
- Notify all threads waiting on the object
notify and notifyAll Who should I use ?
Should be used as much as possible notifyAll, Use notify Because there may be signal loss
Wait timeout mode implements a connection pool
hypothesis The waiting time is T, current time now+T After that, it's time-out
long overtime = now+T;
long remain = T;// The duration of the wait
while(result Not meeting the conditions && remain>0){
wait(remain);
remain = overtime – now;// Wait for the remaining duration
}
return result;
join() Method
Threads A, Thread executed B Of join Method , Threads A You have to wait B When the execution is complete , Threads A To continue your work
call yield() 、sleep()、wait()、notify() What are the effects of methods such as ?
yield()\sleep() Does not release the lock , Just get out of the way cpu The enforcement of
Thread executing yield() in the future , The lock held is not released
sleep() When the method is called , The lock held is not released
Before the transfer method , You have to hold the lock . Called wait() After method , The lock will be released , When wait When the method returns , The thread will hold the lock again
Before the transfer method , You have to hold the lock , call notify() The method itself does not release the lock
边栏推荐
- Pit & ADB wireless debugging of vivo real machine debugging
- 简易入手《SOM神经网络》的本质与原理
- 100 GIS practical application cases (78) - Multi compliance database design and data warehousing
- Osgearth starry background
- [rust notes] 06 package and module
- Mysql容器化(1)Docker安装MySQL
- Downward compatibility and upward compatibility
- MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板
- 图像处理8-CNN图像分类
- VIM learning notes from introduction to silk skating
猜你喜欢
随机推荐
【Rust 笔记】07-结构体
【Rust 笔记】08-枚举与模式
[redis] redis persistent RDB vs AOF (source code)
LinkList
单调栈-503. 下一个更大元素 II
Chocolate installation
Unity editor expansion - scrolling list
[concurrent programming] thread foundation and sharing between threads
Some understandings of 3dfiles
Osgearth starry background
Creation and content of mapnode -- osgearth rendering engine series (2)
简易入手《SOM神经网络》的本质与原理
Notes on understanding applets 2022/7/3
Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
Osgearth topographic shading map drawing
单调栈-42. 接雨水
Jupyter remote server configuration and server startup
Installation of PHP FPM software +openresty cache construction
Unity editor expansion - draw lines
使用base64编码传图片