当前位置:网站首页>Synchronized locked objects
Synchronized locked objects
2022-06-11 03:39:00 【sayWhat_ sayHello】
Preface
Now I'm going to go over it again 《Java The art of concurrent programming 》, There is a passage as follows :
Java Each object in can be used as a lock . Specific for :
- Common synchronization method , The lock is the current instance object .
- Static synchronization method , The lock is for the current class Class object .
- Synchronized method block , A lock is an object configured in parentheses .
Has not been verified , Verify today .
Text
The test code is simple , Just print a sentence , Then sleep 5s. branch 3 Look at the different dump Information .
Common synchronization method
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.helper();
}
public synchronized void helper() {
try {
System.out.println("hello");
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
result :
"main" #1 prio=5 os_prio=0 tid=0x00c46800 nid=0x754 waiting on condition [0x0283f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at com.company.Test.helper(Test.java:16)
- locked <0x04b45e20> (a com.company.Test)
at com.company.Test.main(Test.java:10)
The key is :- locked <0x04b45e20> (a com.company.Test)
Static synchronization method
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.helper();
}
public static synchronized void helper() {
try {
System.out.println("hello");
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
result :
"main" #1 prio=5 os_prio=0 tid=0x013d5c00 nid=0x1b28 waiting on condition [0x02edf000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at com.company.Test.helper(Test.java:14)
- locked <0x05143378> (a java.lang.Class for com.company.Test)
at com.company.Test.main(Test.java:8)
The key is :- locked <0x05143378> (a java.lang.Class for com.company.Test)
Synchronized block
import java.util.concurrent.TimeUnit;
public class Test {
public static String lock = "";
public static void main(String[] args) {
Test test = new Test();
test.helper();
}
public static void helper() {
synchronized (lock) {
try {
System.out.println("hello");
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
result :
"main" #1 prio=5 os_prio=0 tid=0x01336000 nid=0x258c waiting on condition [0x0164f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at com.company.Test.helper(Test.java:16)
- locked <0x050095e0> (a java.lang.String)
at com.company.Test.main(Test.java:9)
The key is :- locked <0x050095e0> (a java.lang.String)
Conclusion
Through these three examples, we can verify the contents of the book :
| form | The object of the lock |
|---|---|
| Synchronization method | a com.company.Test |
| Static synchronization method | a java.lang.Class for com.company.Test |
| A static block ( In brackets is String object ) | a java.lang.String |
边栏推荐
- PostgreSQL source code learning (XX) -- fault recovery ① - transaction log format
- Azure kubernates service update | improve development experience and efficiency
- RT thread test
- OpenGL error Guide
- Product milestones in May 2022
- 正则表达式
- Canvas interactive star animation background JS special effect
- postgresql 捕获函数中的异常
- JS the most commonly used sorting - hand tearing JS series
- Lecturer paging query_ Instructor condition query with page
猜你喜欢

OpenGL第十一章 多光源

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
![[cloud native] what is micro service? How to build it? Teach you how to build the first micro service (framework)](/img/2c/50c692e090d64ab67f7501beb1d989.png)
[cloud native] what is micro service? How to build it? Teach you how to build the first micro service (framework)

net::ERR_ FILE_ NOT_ Found error

OpenGL Chapter 11 multiple light sources

Unity's data persistence -- Jason

/The world of 10 recommended websites for learning programming has entered the era of the Internet. According to a recently released Internet trends 2016 report, China has become a leader in the Inter

PostgreSQL source code learning (18) -- mvcc ③ - creating (obtaining) snapshots

【ELT.ZIP】OpenHarmony啃论文俱乐部——电子设备软件更新压缩

Oppo reno6 turned sour by "inner roll"
随机推荐
Jscpcp L. collecting diamonds (thinking)
Promise use
Mavros控制无人机在gazebo环境下进行双目SLAM
Nsthread of the multithreaded Trilogy
What has TCL done right to break through the technological strength of Chinese brand innovation?
Mavros controls UAV to conduct binocular slam in gazebo environment
Delete the watermark of the picture uploaded by CSDN
postgresql 函数的参数为自定义类型时传参格式
/10个值得推荐的学习编程的网站 世界已经进入了互联网的时代。据最近发布的一篇《2016年互联网趋势》报告显示,中国已成为互联网市场的领导者,中国互联网用户的数量达到了6.68亿。可以预见,有
UML series articles (28) architecture modeling - collaboration
摘桃子(双指针)
Tweenmax colorful ball bouncing animation
【ELT.ZIP】OpenHarmony啃论文俱乐部——多层存储分级数据压缩
Solution to the problem of gd32f4 serial port DMA reception
Checkbox beautify button selected style
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
UML系列文章(28)体系结构建模---协作
2022 年 5 月产品大事记
Shangpinhui mall_ Background homepage of
three.js炫酷科技感背景h5动画