当前位置:网站首页>拨开云雾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
边栏推荐
猜你喜欢
Call wechat customer service applet
Prediction of tomorrow's trading limit of Low Frequency Quantization
VMware15Pro在Deepin系统里面挂载真机电脑硬盘
Windos中安装labellmg教程
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
Check in system based on ibeacons
查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
Wechat applet positioning and ranging through low-power Bluetooth device (2)
帆软中需要设置合计值为0时,一整行都不显示的解决办法
随机推荐
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
VMware 15pro mounts the hard disk of the real computer in the deepin system
基于CM管理的CDH集群集成Phoenix
Gartner预测云迁移规模大幅增长;云迁移的优势是什么?
回顾2021:如何帮助客户扫清上云最后一公里的障碍?
帆软实现分页时第一行和最后两行冻结方式
使用开源调研工具Prophet是一种什么体验?
FBS (fman build system) packaging
HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化
On September 8, the night before going to Songshan Lake
云容灾是什么意思?云容灾和传统容灾的区别?
mysql 5.1升级到5.68
MYSQL 主从同步避坑版教程
Research on recyclerview details - Discussion and repair of recyclerview click dislocation
不同时间类型的执行计划计算
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
云迁移的六大场景
Use of WiFi module based on wechat applet
Storage path of mod subscribed by starbound Creative Workshop at Star boundary