当前位置:网站首页>拨开云雾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
边栏推荐
猜你喜欢
Use of WiFi module based on wechat applet
基于微信小程序的wifi模块使用

修改ddt生成的测试用例名称

Detailed tutorial on the use of smoke sensor (mq-2) (based on raspberry pie 3B +)

Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in

按实际取,每三级分类汇总一次,看图知需求

星界边境文本自动翻译机(高级版)使用说明

帆软中单元格中隔行变色以及数量大于100字体变大变红设置

关于密匙传递的安全性和数字签名

OpenStack如何跨版本升级
随机推荐
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
JSP学习2
redis数据库讲解二(redis高可用、持久化、性能管理)
帆软中单元格中隔行变色以及数量大于100字体变大变红设置
Switch usage (wechat applet)
Visio installation error 1:1935 2: {XXXXXXXX
Chrome插件 之 Selenium IDE、XPath 安装
RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能
jsp学习1
01-NIO基础之ByteBuffer和FileChannel
gzip和gunzip 解压参数详解
Wechat applet positioning and ranging through low-power Bluetooth device (2)
关于密匙传递的安全性和数字签名
RobotFramework 之 用例执行
Wechat applet communicates with low-power Bluetooth - sending data to hardware (III)
教育行业云迁移最佳实践:海云捷迅使用HyperMotion云迁移产品为北京某大学实施渐进式迁移,成功率100%
1到100号的灯开关问题
帆软报表设置单元格填报以及根据值的大小进行排名方法
HyperMotion云迁移助力中国联通,青云完成某央企上云项目,加速该集团核心业务系统上云进程
MySQL数据库讲解(七)