当前位置:网站首页>Five ways of using synchronized to remove clouds and fog are introduced
Five ways of using synchronized to remove clouds and fog are introduced
2022-04-23 14:14:00 【pureluckyfish】
The smaller the range of the lock , Minimal impact on code execution efficiency . The best way is to leave it unlocked , Concurrent programming is not always thread safe , Only when multiple threads share the same instance variable can thread safety problems occur . Non thread safety issues require locking for synchronization .
1、synchronized Method
Solved the problem of thread safety , But it affects the execution efficiency ;synchronized Method The range of the lock is the largest , So the execution efficiency is also the slowest .
synchronized public void printA() {
try {
System.out.println(" The thread name is :"+Thread.currentThread().getName()+" stay "+System.currentTimeMillis()+" Get into printA");
Thread.sleep(3000);
System.out.println(" The thread name is :"+Thread.currentThread().getName()+" stay "+System.currentTimeMillis()+" Leave printA");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2、synchronized static Method
every last *.java File corresponding to the class Class is singleton in memory .synchronized static Method It's right *.java File corresponding Class Class object ;synchronized Method Is to lock the instance object of the class where the method is located , They are two different locks .
synchronized static void printC() {
try {
System.out.println(" The thread name is :"+Thread.currentThread().getName()+" stay "+System.currentTimeMillis()+" Get into printC");
System.out.println(" The thread name is :"+Thread.currentThread().getName()+" stay "+System.currentTimeMillis()+" Leave printC");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3、synchronized(xxx.class) Code block :
synchronized(xxx.class) Code block It can work on all object instances of the class .
public void printC() {
synchronized (Service1.class) {
System.out.println(" The thread name is :" + Thread.currentThread().getName() + " stay " + System.currentTimeMillis() + " Get into printC");
System.out.println(" The thread name is :" + Thread.currentThread().getName() + " stay " + System.currentTimeMillis() + " Leave printC");
}
}
4、synchronized(this) Code block :
Locked is the current object , contrast synchronized Method , Reduced lock range , That is to reduce the scope of synchronization code , Thus, the efficiency of program execution is improved .
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
5、synchronized ( Not this object ) Code block :
The advantage is that there are two locks , Not with other synchronized(this) Scramble for this lock , Reduce the scope of synchronization , Greatly improve operation efficiency .
package com.yu.syn;
public class Service2 {
private String usernameParam;
private String passwordParam;
private String anything = new String();
public void setUsernamePassword(String username, String password) {
String anything = new String();
try {
synchronized (anything) {
System.out.println(" The thread name is :" + Thread.currentThread().getName() + " stay " + System.currentTimeMillis() + " Enter synchronization fast ");
usernameParam = username;
Thread.sleep(3000);
passwordParam = password;
System.out.println(" The thread name is :" + Thread.currentThread().getName() + " stay " + System.currentTimeMillis() + " Leave sync fast ");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
版权声明
本文为[pureluckyfish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231406486177.html
边栏推荐
- How QT designer adds resource files
- 帆软中需要设置合计值为0时,一整行都不显示的解决办法
- 线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
- Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
- 贷款市场报价利率(LPR)与贷款基准利率介绍
- 帆软调用动态传参的方法,在标题中设置参数
- sql中出现一个变态问题
- MySQL数据库讲解(九)
- 不同时间类型的执行计划计算
- 金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务
猜你喜欢
随机推荐
服务器日志分析工具(识别,提取,合并,统计异常信息)
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
How QT designer adds resource files
redis数据库讲解二(redis高可用、持久化、性能管理)
yml引用其他变量
Detailed tutorial on the use of setinterval timing function of wechat applet
HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化
Visio installation error 1:1935 2: {XXXXXXXX
在Clion中给主函数传入外部参数
Algorithem_ReverseLinkedList
mysql 5.1升级到5.610
按实际取,每三级分类汇总一次,看图知需求
GFS分布式文件系统(理论)
redis数据库讲解(四)主从复制、哨兵、Cluster群集
dp-[NOIP2000]方格取数
Mysql的安装过程(已经安装成功的步骤说明)
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
帆软报表设置单元格填报以及根据值的大小进行排名方法
Visio画拓扑图随记