当前位置:网站首页>拨开云雾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
边栏推荐
猜你喜欢
VMware 15pro mounts the hard disk of the real computer in the deepin system
什么是云迁移?云迁移的四种模式分别是?
Kettle -- control parsing
帆软中使用if else 进行判断-使用标题条件进行判断
帆软中需要设置合计值为0时,一整行都不显示的解决办法
HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化
多云数据流转?云上容灾?年前最后的价值内容分享
Check in system based on ibeacons
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
sql中出现一个变态问题
随机推荐
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
基于CM管理的CDH集群集成Phoenix
Wechat applet communicates with esp8266 based on UDP protocol
VMware15Pro在Deepin系统里面挂载真机电脑硬盘
Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)
VMWare安装64位XP中文教程
星界边境文本自动翻译机(高级版)使用说明
On September 8, the night before going to Songshan Lake
mysql 5.1升级到5.610
Call wechat customer service applet
JSP学习2
PySide2
ActiveMq基础知识
Wechat applet communicates with low-power Bluetooth - receives data sent by hardware (IV)
redis数据库讲解(三)redis数据类型
使用itextpdf实现截取pdf文档第几页到第几页,进行分片
Operation instructions of star boundary automatic text translator (advanced version)
rsync+inotify远程同步
VMware 15pro mounts the hard disk of the real computer in the deepin system
Some good articles on pthread multithreading