当前位置:网站首页>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 catch exception in function
- three.js炫酷科技感背景h5动画
- Lecturer paging query_ Instructor condition query with page
- Azure kubernates service update | improve development experience and efficiency
- Thinkphp3.2.3 deserialization using chain analysis
- Oppo reno6 turned sour by "inner roll"
- 基于SSM框架的连锁超市购物零售后台管理系统
- js最常用的排序---手撕js系列
- OPENSSL ASN.1, DER, PEM, X509
- J. Balanced Tree
猜你喜欢

The key data of music genuine rate is missing. What is the odds of Netease cloud music IPO?

突破中国品牌创新技术实力,TCL做对了什么?

音乐正版率关键数据缺失,网易云音乐IPO胜算几何?

net::ERR_ FILE_ NOT_ Found error

SQL查询连续三天登录的用户

UML系列文章(28)体系结构建模---协作
![[elt.zip] openharmony paper Club - Data high-throughput lossless compression scheme](/img/ed/4862c937f2d50bcf5d977527a143a6.png)
[elt.zip] openharmony paper Club - Data high-throughput lossless compression scheme

多线程四部曲之NSThread

Unity's data persistence -- Jason

潮玩力真火力!年轻人第一台巨幕影院?酷开电视Max 86“庞然来袭
随机推荐
UML系列文章(28)体系结构建模---协作
2022 年 5 月产品大事记
regular expression
LaTex环境下在TexStudio中使用minted插入高亮代码
对象存储Minio使用教程
PostgreSQL source code learning (18) -- mvcc ③ - creating (obtaining) snapshots
OpenGl第十章 投光物
What has TCL done right to break through the technological strength of Chinese brand innovation?
SQL | some indicators of the game industry
J. Balanced Tree
J. Balanced Tree
postgresql 捕获函数中的异常
Nsthread of the multithreaded Trilogy
The tide play power is really firepower! The first big screen cinema for young people? Cool open TV Max 86 "sudden attack
Parameter transfer format when the parameter of PostgreSQL function is a user-defined type
OpenGL第十一章 多光源
PostgreSQL source code learning (17) -- mvcc ② - Introduction to snapshot and isolation level
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
CheckBox美化按钮选中样式
实现发布订阅模式-----手撕js系列