当前位置:网站首页>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
边栏推荐
- ActiveMq基础知识
- rsync+inotify远程同步
- krpano全景之vtour文件夹和tour
- How to do a project easily
- Wechat applet communicates with low-power Bluetooth - receives data sent by hardware (IV)
- Can global variables be defined in header files
- Visio installation error 1:1935 2: {XXXXXXXX
- 按实际取,每三级分类汇总一次,看图知需求
- OpenStack命令操作
- 回顾2021:如何帮助客户扫清上云最后一公里的障碍?
猜你喜欢
随机推荐
帆软中根据分类进行汇总
贷款市场报价利率(LPR)与贷款基准利率介绍
不同时间类型的执行计划计算
Request module
政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
第四届“传智杯”全国大学生IT技能大赛(决赛B组) 题解
Essential difference between restful WebService and gSOAP webservice
百度图片识别自定义实现(替代AipOcr)
Can I compile the header file and source file of the template separately
在MAC上安装mysql
MySQL数据库讲解(九)
帆软中使用if else 进行判断-使用标题条件进行判断
帆软分割求解:一段字符串,只取其中某个字符(所需要的字段)
Pass in external parameters to the main function in clion
OpenStack命令操作
JDBC详解
void*是怎样的存在?
On September 8, the night before going to Songshan Lake
krpano全景之vtour文件夹和tour
帆软中需要设置合计值为0时,一整行都不显示的解决办法









