当前位置:网站首页>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 .
边栏推荐
- [development of large e-commerce projects] performance pressure test - Performance Monitoring - heap memory and garbage collection -39
- JS implementation determines whether the point is within the polygon range
- A simple SSO unified login design
- Database postragesq PAM authentication
- Game 280 of leetcode week
- Global and Chinese markets of radiation linear accelerators 2022-2028: Research Report on technology, participants, trends, market size and share
- One plus six brushes into Kali nethunter
- 微信小程序;胡言乱语生成器
- 力扣剑指offer——二叉树篇
- Database postragesql client authentication
猜你喜欢

Do you know the eight signs of a team becoming agile?
![[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #](/img/1c/deaf20d46e172af4d5e11c28c254cf.jpg)
[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #

Main window in QT application

DOM basic syntax

微信小程序:独立后台带分销功能月老办事处交友盲盒

A simple SSO unified login design

Four pits in reentrantlock!

Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #

One plus six brushes into Kali nethunter

Phpstrom setting function annotation description
随机推荐
Wechat applet; Gibberish generator
A simple SSO unified login design
The server time zone value ‘� й ��� ʱ 'is unrecognized or representatives more than one time zone【
Vulnstack3
PowerShell: use PowerShell behind the proxy server
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Wechat applet: independent background with distribution function, Yuelao office blind box for making friends
Exploration and Practice of Stream Batch Integration in JD
What is the current situation and Prospect of the software testing industry in 2022?
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
Database postragesq peer authentication
One click generation and conversion of markdown directory to word format
[development of large e-commerce projects] performance pressure test - Optimization - impact of middleware on performance -40
142. Circular linked list II
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
ROS command line tool
JS implementation determines whether the point is within the polygon range
Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?
La jeunesse sans rancune de Xi Murong