当前位置:网站首页>拨开云雾synchronized使用五种方式介绍
拨开云雾synchronized使用五种方式介绍
2022-04-23 14:07:00 【pureluckyfish】
锁的范围越小,对代码执行效率的影响最小。最好的方式就是不加锁,并发编程不一定都是非线程安全的,只有多线程共享同一实例变量才有可能会出现线程安全问题。非线程安全问题才需要加锁进行同步。
1、synchronized 方法
解决了线程安全的问题,但影响执行效率;synchronized 方法 锁的范围是最大的,所以执行效率也是最慢的。
synchronized public void printA() {
try {
System.out.println("线程名称为:"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"进入printA");
Thread.sleep(3000);
System.out.println("线程名称为:"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"离开printA");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2、synchronized static 方法
每一个 *.java 文件对应class类的实例在内存中是单例的。synchronized static 方法 是对 *.java 文件对应的Class类对象进行持锁;synchronized 方法 是将方法所在类的实例对象为锁,俩者是俩把不同的锁。
synchronized static void printC() {
try {
System.out.println("线程名称为:"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"进入printC");
System.out.println("线程名称为:"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"离开printC");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3、synchronized(xxx.class)代码块:
synchronized(xxx.class)代码块可以对类的所有对象实例起作用。
public void printC() {
synchronized (Service1.class) {
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "进入printC");
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "离开printC");
}
}
4、synchronized(this)代码块:
锁定的是当前对象,对比 synchronized 方法,减少了锁的范围,也就是减少了同步代码的范围,从而提高了程序执行效率。
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
5、synchronized (非this对象)代码块:
优点是存在俩把锁,不与其他 synchronized(this)争抢 this 锁,减少同步的范围,大大提高运行效率。
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("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "进入同步快");
usernameParam = username;
Thread.sleep(3000);
passwordParam = password;
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "离开同步快");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
版权声明
本文为[pureluckyfish]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sinat_33918956/article/details/121922445
边栏推荐
猜你喜欢
Research on recyclerview details - Discussion and repair of recyclerview click dislocation
关于云容灾,你需要知道这些
关于密匙传递的安全性和数字签名
星界边境Starbound创意工坊订阅的mod的存放路径
Call wechat customer service applet
CDH cluster integration Phoenix based on CM management
OpenStack命令操作
Gartner预测云迁移规模大幅增长;云迁移的优势是什么?
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
帆软中需要设置合计值为0时,一整行都不显示的解决办法
随机推荐
Mock测试
ActiveMq基础知识
Essential difference between restful WebService and gSOAP webservice
HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化
MYSQL 主从同步避坑版教程
Switch usage (wechat applet)
Detailed tutorial on the use of setinterval timing function of wechat applet
postman批量生产body信息(实现批量修改数据)
findstr不是内部或外部命令解决方法
log4j 输出日志信息到文件中
VMware Workstation 无法连接到虚拟机。系统找不到指定的文件
Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)
On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
redis数据库讲解二(redis高可用、持久化、性能管理)
RecyclerView高级使用(一)-侧滑删除的简单实现
openstack理论知识
使用itextpdf实现截取pdf文档第几页到第几页,进行分片
帆软报表设置单元格填报以及根据值的大小进行排名方法
PySide2