搜索
查看
编辑修改
首页
UNITY
NODEJS
PYTHON
AI
GIT
PHP
GO
CEF3
JAVA
HTML
CSS
搜索
AllinToyou
这个屌丝很懒,什么也没留下!
关注作者
热门标签
jquery
HTML
CSS
PHP
ASP
PYTHON
GO
AI
C
C++
C#
PHOTOSHOP
UNITY
iOS
android
vue
xml
爬虫
SEO
LINUX
WINDOWS
JAVA
MFC
CEF3
CAD
NODEJS
GIT
Pyppeteer
article
热门文章
1
【LeetCode】热题100:LRU缓存
2
Pytest+selenium UI自动化测试实战实例(超详细)_selenium自动化测案例python
3
Python(19)Excel表格操作Ⅰ_python操作表格的库
4
Source Insight 工程中添加.S文件_source insight添加.s文件
5
DVWA —— Brute Force 暴力破解_dvwa brute force
6
硬件探索——2FSK通信系统调制解调综合实验电路设计_2fsk相干解调各点时间波形
7
linux raid autodetect,软raid的建立
8
Android 13 SystemUI 的四种通知(Notification)视图
9
《数据结构与算法之美》读后感——03(下)
10
基于树莓派4B的YOLOv5-Lite目标检测的移植与部署(含训练教程)
当前位置:
article
> 正文
Java synchronized 对象锁与类锁的区别、同步代码块与同步方法的区别 详解_对象锁与类锁能否同步
作者:AllinToyou | 2024-04-13 00:09:37
赞
踩
对象锁与类锁能否同步
java 内置锁 : 每个 java对象 都可以用做一个实现同步的锁,这些锁成为内置锁。线程进入同步代码块或方法的时候会自动获得该锁,在退出同步代码块或方法时会释放该锁。获得内置锁的唯一途径就是进入这个锁的保护的同步代码块或方法。java 内置锁是一个互斥锁,这就是意味着最多只有一个线程能够获得该锁,当 线程A 尝试去获得 线程B 持有的内置锁时,线程A 必须等待或者阻塞,直到 线程B 释放这个锁,如果B线程不释放这个锁,那么 A线程 将永远等待下去
java 对象锁和类锁 : java 的 对象锁 和 类锁 在锁的概念上基本上和内置锁是一致的,但是,两个锁实际是有很大的区别的,对象锁是用于对象实例方法,或者一个对象实例上的,类锁是用于类的静态方法或者一个类的 class对象上的。类的对象实例可以有很多个,但是每个类只有一个class对象,所以不同对象实例的对象锁是互不干扰的,但是每个类只有一个类锁。但是有一点必须注意的是,其实类锁只是一个概念上的东西,并不是真实存在的,它只是用来帮助我们理解锁定实例方法和静态方法的区别的
类的 非静态方法锁 和 内置锁是同一把锁,也就是都是 对象锁。因此当对象执行到 同步代码块 时这个对象的另外一个线程由于无法获得对象锁因此无法执行这个对象的所有使用 synchronized 修饰的非静态方法
public class TestSynchronized1 {
public void test1() {
synchronized (this) {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
}
}
public synchronized void test2() {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
}
public static void main(String[] args) {
final TestSynchronized1 myt2 = new TestSynchronized1();
Thread test1 = new Thread(myt2::test1, "test1");
Thread test2 = new Thread(myt2::test2, "test2");
test1.start();
test2.start();
// 输出 :
// test1 : 4
// test1 : 3
// test1 : 2
// test1 : 1
// test1 : 0
// test2 : 4
// test2 : 3
// test2 : 2
// test2 : 1
// test2 : 0
}
}
public class TestSynchronized4 {
class Inner {
private void m4t1() {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : Inner.m4t1()=" + i);
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
}
private synchronized void m4t2() {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : Inner.m4t2()=" + i);
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
}
}
private void m4t1(Inner inner) {
synchronized (inner) {
// 使用对象锁
inner.m4t1();
}
}
private void m4t2(Inner inner) {
inner.m4t2();
}
public static void main(String[] args) {
final TestSynchronized4 myt3 = new TestSynchronized4();
final Inner inner = myt3.new Inner();
Thread t1 = new Thread(() -> myt3.m4t1(inner), "t1");
Thread t2 = new Thread(() -> myt3.m4t2(inner), "t2");
t1.start();
t2.start();
// 输出 :
// t1 : Inner.m4t1()=4
// t1 : Inner.m4t1()=3
// t1 : Inner.m4t1()=2
// t1 : Inner.m4t1()=1
// t1 : Inner.m4t1()=0
// t2 : Inner.m4t2()=4
// t2 : Inner.m4t2()=3
// t2 : Inner.m4t2()=2
// t2 : Inner.m4t2()=1
// t2 : Inner.m4t2()=0
}
}
静态方法锁 就是 类锁
public class TestSynchronized2 {
public void test1() {
synchronized (TestSynchronized2.class) {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
}
}
public static synchronized void test2() {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
}
public static void main(String[] args) {
final TestSynchronized2 myt2 = new TestSynchronized2();
Thread test1 = new Thread(myt2::test1, "test1");
Thread test2 = new Thread(TestSynchronized2::test2, "test2");
test1.start();
test2.start();
// 输出 :
// test1 : 4
// test1 : 3
// test1 : 2
// test1 : 1
// test1 : 0
// test2 : 4
// test2 : 3
// test2 : 2
// test2 : 1
// test2 : 0
}
}
类锁 和 对象锁不是通一把锁,相互之间互不干扰
public class TestSynchronized3 {
public synchronized void test1() {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
}
public static synchronized void test2() {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
}
public static void main(String[] args) {
final TestSynchronized3 myt2 = new TestSynchronized3();
Thread test1 = new Thread(myt2::test1, "test1");
Thread test2 = new Thread(TestSynchronized3::test2, "test2");
test1.start();
test2.start();
// 输出 :
// test1 : 4
// test2 : 4
// test2 : 3
// test1 : 3
// test2 : 2
// test1 : 2
// test2 : 1
// test1 : 1
// test1 : 0
// test2 : 0
}
}
此处会先输出 showA.. showC.. 等待 3秒后再输出 showB..,可以使用 synchronized 代码块或方法只有获取到响应的锁之后才能执行相应的方法
public class TestSynchronized5 {
public synchronized void showA() {
System.out.println("showA..");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void showB() {
synchronized (this) {
System.out.println("showB..");
}
}
public void showC() {
String s = "1";
synchronized (s) {
System.out.println("showC..");
}
}
public static void main(String[] args) {
final TestSynchronized5 sy = new TestSynchronized5();
new Thread(sy::showA).start();
new Thread(sy::showB).start();
new Thread(sy::showC).start();
// 输出 :
// showA..
// showC..
// showB..
}
}
1> 当两个并发线程访问同一个对象 object 中的这个 synchronized(this) 同步代码块时,一个时间内只能有一个线程得到执行,另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块
2> 然而,当一个线程访问 object 的一个 synchronized(this) 同步代码块时,另一个线程仍然可以访问该 object 中的非synchronized(this)同步代码块
3> 尤其关键的是,当一个线程访问object的一个 synchronized(this) 同步代码块时,其他线程对 object 中所有其它 synchronized(this) 同步代码块 或 synchronized 修饰的非静态方法 的访问将被阻塞
4> 对象锁和类锁可以同时获得,即两种方法可以同时运行,互不影响
5> 一种锁方法只能运行同时运行一个,同种其他的锁方法暂时阻塞,普通方法的运行不受限制
声明:
本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:
https://www.wpsshop.cn/w/AllinToyou/article/detail/413334
推荐阅读
article
Hive
、
Pig
、
HBase
的关系与区别,值得收藏!_
hbase
,
hive
,
pig...
欢迎关注大数据和人工智能技术文章发布的微信公众号:清研学堂,在这里你可以学到夜白(作者笔名)精心整理的笔记,让我们每天进...
赞
踩
article
ubuntu
主机
适配
安装
adb
工具与
rk3588
连接...
ubuntu
主机
适配
安装
adb
工具与
rk3588
连接
ubuntu
主机
适配
安装
adb
工具与
rk3588
连接 ...
赞
踩
article
SourceTree
免
登录
跳过
初始设置_
sourtree
跳过
登录
...
SourceTree
免
登录
跳过
初始设置_
sourtree
跳过
登录
sourtree
跳过
登录
用于...
赞
踩
article
CleanMy
Mac
X 4.15.1– 专业
Mac
清理
软件
:
清理
系统垃圾、卸载
恶意
软件
、优化 ...
相信不少的小伙伴都在用苹果电脑,不论是 i
Mac
,还是
Mac
Book,用着用着电脑就变慢了。这通病与苹果电脑的性能无关...
赞
踩
article
数据
统计
与
分析
基础
实验
一:基本语法与运算(R
语言
)_r
语言
程序设计
实验
练习...
数据
分析
与
统计
基础
实验
一:基本语法与运算编写程序,记录十名学生的信息,至少包括姓名、年龄、出生年、
数据
统计
分析
课程
实验
成...
赞
踩
article
用
Python
开发
植物
大战
僵尸
游戏
_基于
python
的
植物
大战
僵尸
游戏
设计与实现.doc...
开发思路1.引入需要的模块,配置图片路径,设置界面宽高背景颜色,创建
游戏
主入口。#1引入需要的模块import pyga...
赞
踩
article
【教程】
idea
中
github
copliot插件无法登录连接,报错Sign
in
fai
led
.R...
在
idea
当中登录copliot时(即点击Log
in
to GitHub),在长时间读条后连接失败,报错提示为Sign ...
赞
踩
article
人工智能
发展
飞速
,
未来几十年哪些
职业
会被逐渐
取代
,
这些
专业
还值得选择吗?...
随着当今科学技术的飞速
发展
,
除了给人们生活带来便利外
,
也使不少人生活受到威胁
,
就好比
人工智能
的异军突起
,
在其高效完成任务...
赞
踩
article
动态
规划
(DP)之入门学习-
数字
三角形
_
数字
三角形
问题
...
数字
三角形
案例题目描述 Description下图给出了一个
数字
三角形
,请编写一个程序,计算从顶至底的某处的一条路径,使...
赞
踩
article
animation
-
duration
: 2s有什么问题?
为什么
pycharm
提示
错误
?...
另外,如果你使用的是SCSS或Less等CSS预处理器,那么PyCharm可能无法正确解析和识别预处理器中的特定语法。如...
赞
踩
article
flink
之
Operator
State
(non-
keyed
state
)...
flink
介绍,
flink
对所有的算子都支持有状态计算,在博主之前分享的文章中,关于
keyed
state
已经做过很详细...
赞
踩
article
机器学习实战----
使用
Python
和
Scikit
-
Learn
构建简单
分类器
_用
python
实现简单...
今天我们将学习
使用
Python
和
Scikit
-
Learn
创建一个简单的文本
分类器
来识别垃圾邮件。我们将先介绍数据集,并通...
赞
踩
article
训练
自己的
GPT2
模型
(
中文
),踩坑与经验_
gpt2
训练
...
GPT
模型
,
中文
,自己微调,经验与踩坑_
gpt2
训练
gpt2
训练
...
赞
踩
article
开发
那点事(十三)您
的
微信
小
程序
代码
被扒盗用上架
,
如何保证
代码
安全_
微信
小
程序
代码
保护...
写在前面
的
话
小
编前段时间刷博客
的
时候看到了一篇这样
的
文章
,
大概
的
内容是
开发
者辛辛苦苦
开发
小
半年
的
作品
,
上线没几天
,
就被人...
赞
踩
article
【
Hadoop
】本地
文件
上
传
到
HDFS
_
上
传
文件
到
hdfs
...
需求:按照配置
文件
的内容解析要
上
传
文件
的路径,
HDFS
的目标路径,
文件
名格式匹配,按照不同的
文件
名
上
传
到不同的
HDFS
目...
赞
踩
article
高中
学历
的
程序员
,
以包装
的
方式进入现在
的
公司
,
想跳槽咋办?_
学历
包装...
一看到这条消息
的
时候我就感觉可能是培训出来
的
或者是自学
的
,
众所周知现在广州
的
IT培训机构数不胜数
,
几乎每个月就有几千个程...
赞
踩
article
Apollo
源码
剖析_
apollo
源码
分析...
Apollo
源码
剖析1. Portal创建APP1.1 创建APP1.1.1 实体Bean1. Portal创建APPA...
赞
踩
article
【计算机
毕业设计
】基于
Java
的
超市
进销存
系统
_
java
进销存
系统
...
本
超市
进销存
系统
以springboot作为框架,b/s模式以及MySql作为后台运行的数据库,同时使用Tomcat用为系...
赞
踩
article
职场
六忌_
工作
六忌...
俗话说,“有人的地方就有江湖”。在
职场
中
工作
,需要跟领导和同事处好关系,营造出一个和谐的
工作
环境,让自己的职业发展更加顺...
赞
踩
article
S
ource
In
s
ight
工程中添加.
S
文件_
s
ource in
s
ight添加.
s
文件...
S
ource
In
s
ight
工程中添加.
S
文件_
s
ource in
s
ight添加.
s
文件
s
ource in
s
ight添...
赞
踩
相关标签
hbase
hive
hadoop
ubuntu
adb
linux
运维
git
macos
CleanMyMac X
CleanMyMac
数据分析
数据挖掘
intellij-idea
java
ide
github
copilot
chatgpt
编程
程序员
c/c++
学习
人工智能