当前位置:网站首页>JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
2022-07-05 01:42:00 【A bowl of humble powder】
One 、 problem
【 ask 】 What happens when multiple threads initialize a class at the same time
【 answer 】 Only one thread is allowed to perform initialization , Other threads will block .
JVM Will guarantee a class of <clinit>() Methods are properly locked in a multithreaded environment , Sync .
Two 、 verification
【 Code 】
class Loader{
static {
// Blocking method
System.out.println(" Initialization !!!");
if (true){
while (true){
//System.out.println(" Blocking Lala ");
}
}
}
}
public class CInit {
public static void main(String[] args) {
Runnable run = new Runnable(){
public void run() {
// Create objects
System.out.println(Thread.currentThread().getName() + "《 Start creating objects 》");
Loader loader = new Loader();
System.out.println(Thread.currentThread().getName() + "《 End creating object 》");
}
};
new Thread(run).start(); // First thread
new Thread(run).start(); // The second thread
new Thread(run).start(); // The third thread
}
}
【 Console output 】

3、 ... and 、 Conclusion
You can see from the results output from the console , Only one of the three threads has entered 【 initialization 】 in
【 Conclusion 】 If multiple threads initialize a class at the same time , There will only be one thread to initialize , Other threads are blocked .
边栏推荐
- batchnorm.py这个文件单GPU运行报错解决
- es使用collapseBuilder去重和只返回某个字段
- Change the background color of a pop-up dialog
- If the consumer Internet is compared to a "Lake", the industrial Internet is a vast "ocean"
- Win:使用 Shadow Mode 查看远程用户的桌面会话
- 微信小程序:全网独家小程序版本独立微信社群人脉
- Basic operations of database and table ----- create index
- 220213c language learning diary
- 【CTF】AWDP总结(Web)
- Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
猜你喜欢

Incremental backup? db full

JS implementation determines whether the point is within the polygon range

Yyds dry inventory swagger positioning problem ⽅ formula
![[development of large e-commerce projects] performance pressure test - Optimization - impact of middleware on performance -40](/img/e4/0bdee782a65028b2bc87db85d48738.png)
[development of large e-commerce projects] performance pressure test - Optimization - impact of middleware on performance -40

微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益

【大型电商项目开发】性能压测-优化-中间件对性能的影响-40

Nebula importer data import practice

增量备份 ?db full

Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?

After reading the average code written by Microsoft God, I realized that I was still too young
随机推荐
[swagger]-swagger learning
runc hang 导致 Kubernetes 节点 NotReady
Wechat applet: independent background with distribution function, Yuelao office blind box for making friends
Four pits in reentrantlock!
What is the length of SHA512 hash string- What is the length of a hashed string with SHA512?
Database postragesq PAM authentication
Global and Chinese markets for stratospheric UAV payloads 2022-2028: Research Report on technology, participants, trends, market size and share
Outlook: always prompt for user password
流批一體在京東的探索與實踐
Global and Chinese markets for industrial X-ray testing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of radiation linear accelerators 2022-2028: Research Report on technology, participants, trends, market size and share
Discrete mathematics: reasoning rules
Global and Chinese market of nutrient analyzer 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of portable CNC cutting machines 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of emergency rescue vessels (errv) 2022-2028: Research Report on technology, participants, trends, market size and share
Win:使用 Shadow Mode 查看远程用户的桌面会话
Global and Chinese market of veterinary thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
JS implementation determines whether the point is within the polygon range
Heartless sword English translation of Xi Murong's youth without complaint