当前位置:网站首页>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 .
边栏推荐
- Wechat applet; Gibberish generator
- Unified blog writing environment
- Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
- Five ways to query MySQL field comments!
- 微信小程序:全新独立后台月老办事处一元交友盲盒
- Roads and routes -- dfs+topsort+dijkstra+ mapping
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Database postragesq peer authentication
- Win: add general users to the local admins group
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
猜你喜欢
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Application and Optimization Practice of redis in vivo push platform
A simple SSO unified login design
微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
Interesting practice of robot programming 15- autoavoidobstacles
Yyds dry goods inventory kubernetes management business configuration methods? (08)
Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Wechat applet; Gibberish generator
[swagger]-swagger learning
随机推荐
Educational Codeforces Round 122 (Rated for Div. 2) ABC
PHP 基础篇 - PHP 中 DES 加解密详解
Common bit operation skills of C speech
流批一體在京東的探索與實踐
力扣剑指offer——二叉树篇
微信小程序:全新独立后台月老办事处一元交友盲盒
Outlook:总是提示输入用户密码
如果消费互联网比喻成「湖泊」的话,产业互联网则是广阔的「海洋」
Numpy library introductory tutorial: basic knowledge summary
Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
Heartless sword English translation of Xi Murong's youth without complaint
batchnorm. Py this file single GPU operation error solution
Huawei machine test question: longest continuous subsequence
PHP 约瑟夫环问题
Yyds dry inventory swagger positioning problem ⽅ formula
当产业互联网时代真正发展完善之后,将会在每一个场景见证巨头的诞生
Database postragesq role membership
如何搭建一支搞垮公司的技术团队?
Can financial products be redeemed in advance?
Armv8-a programming guide MMU (3)