当前位置:网站首页>Advanced multithreading
Advanced multithreading
2022-04-22 14:08:00 【Aeolian u】
Multi thread advanced
Common lock strategies
1 Optimism lock vs Pessimistic locking

2 Read-write lock vs Common mutex
Between threads , There is no thread safety problem between data readers , But data writers need to be mutually exclusive with each other and with readers . If the same lock is used in both scenarios , There will be great performance loss . Therefore, read-write locks are generated .

A thread's access to data , There are two main operations : Reading data and Writing data .
Both threads just read one data , There is no thread safety issue at this time . Read directly and concurrently .
Both threads have to write a data , There are thread safety issues .
One thread reads and another thread writes , There are also thread safety issues
Between read lock and read lock , Not mutually exclusive .
Between write lock and write lock , Mutually exclusive .
Between read lock and write lock , Mutually exclusive .
The read-write lock is especially suitable for “ Frequent reading , Write less frequently ” In the scene . ( Such scenes are actually very widespread )
3 Hang wait lock vs spinlocks

4 Heavyweight lock vs Lightweight lock

5 Fair lock vs Not fair lock

6 Reentrant lock vs Do not reenter the lock

synchronized The above summary

CAS




like :



like :

ABA problem




ABA The solution to the problem




synchronized Lock optimization mechanism in ( Several typical optimization methods )
Lock expansion / Lock escalation

It's like :

Lock coarsening

Lock elimination

Callable Interface
Callable It's a interface . It is equivalent to encapsulating a thread “ Return value ”. It is convenient for the program to calculate the result by means of multithreading .

Use Callable Interface
package Callable Interface ;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/** * @author: fs * @date: 2022/4/20 20:53 * @Description: */
public class Demo {
public static void main(String[] args){
// adopt Callable To describe such a task
Callable<Integer> callable=new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int sum=0;
for (int i = 0; i <= 1000; i++) {
sum += i;
}
return sum;
}
};
// In order for the thread to execute callable The task , The use of construction methods alone is not enough , You also need an auxiliary class .
FutureTask<Integer> task=new FutureTask(callable);
// Create a thread to complete the calculation here
Thread t=new Thread(task);
t.start();
// Bring your own spicy hot with a small ticket 、
// What if the task of the thread is not finished ,get_ It will block .
// It's blocked until , Mission accomplished , The result is calculated ~~
try {
System.out.println(task.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Description of the above code

JUC(java.util.concurrent) Common classes for
ReentrantLock
Reentrant mutexes . and synchronized Similar location , Are used to achieve mutually exclusive effects , Ensure thread safety .
ReentrantLock It's also a re-entry lock . “Reentrant” The original meaning of this word is “ Reentrant ”
ReentrantLock Usage of :
lock(): Lock , If you can't get the lock, you'll die and wait .
trylock( Timeout time ): Lock , If the lock cannot be acquired , Give up locking after waiting for a certain time .
unlock(): Unlock


CountDownLatch



Multithreading environment uses ArrayList


Multithreaded environments use hash tables


like :

Solution

summary

版权声明
本文为[Aeolian u]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221407012513.html
边栏推荐
- 嵌入式软件bug从哪来,怎么去
- [finally waiting for you] wechat voice forwarding method - voice message forwarding
- OceanBase的超级用户 - 系统租户sys
- Single stage and two-stage target detection
- 【机器人学习】移动机器人里程计学习记录
- 获取数据库中数值时,数据库有值,却为空??
- Genesis public chain gives Dao new vitality
- 微信小程序商城项目实战(第六篇:商品搜索)
- 如何使用OpenFeign实现对于第三方接口的调用
- CVPR 2022 Oral | 大连理工提出小样本识别DeepBDC,6项基准性能最好
猜你喜欢

华为云媒体査勇:华为云在视频AI转码领域的技术实践
![uniapp转微信开发者工具报错 - [ app.json 文件内容错误] app.json: 未找到 [“sitemapLocation“] 对应的 sitemap.json 文件](/img/3b/2f371eab7d2f9e976dcd4612a19518.png)
uniapp转微信开发者工具报错 - [ app.json 文件内容错误] app.json: 未找到 [“sitemapLocation“] 对应的 sitemap.json 文件

Chapitre sur la promotion précise des investissements dans la gestion de l'exploitation numérique des parcs industriels

獲取數據庫中數值時,數據庫有值,卻為空??

Cannot read property 'forceupdate' of undefined - wechat developer tool reports an error

In February, I relied on this PDF document to interview bat. Unexpectedly, I received five offers

Golang:包管理

uniapp转微信小程序报错Cannot read property ‘forceUpdate‘ of undefined - 微信开发者工具报错

Fastdfs installation and configuration

线程池--
随机推荐
An error is reported when reading the attribute value of the object through the variable storage key value in TS (TS: 7053)
独立站运营 | 6个Facebook推广小技巧,你都知道吗?
C Primer Plus---程序清单13.2 reduto.c
Single stage and two-stage target detection
P2B论文复现——点云学习记录
In the source code of Vue cache compilation results, the template is used as the cache key, and the template written by the user is so long. Is this appropriate
产业园区数字化运营管理之“精准招商”篇
CPT 104_ Lab 09
2022危险化学品经营单位安全管理人员操作证考试题及模拟考试
Chapitre sur la promotion précise des investissements dans la gestion de l'exploitation numérique des parcs industriels
uniapp运行到小程序模拟器的方法 - uniapp开启微信开发者工具预览支持 - HBuilderX
[finally waiting for you] wechat voice forwarding method - voice message forwarding
How to use openfeign to call the third-party interface
BCC-stackcount
C Primer Plus --- program list 13.2 reduto c
阻塞队列-
Actual combat of wechat applet mall project (Part 6: commodity search)
Fastdfs installation and configuration
獲取數據庫中數值時,數據庫有值,卻為空??
银行为什么要上堡垒机?选择哪家好?有案例吗?