当前位置:网站首页>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 .
边栏推荐
- Global and Chinese markets for stratospheric UAV payloads 2022-2028: Research Report on technology, participants, trends, market size and share
- Game 280 of leetcode week
- 无心剑英译席慕容《无怨的青春》
- Yyds dry inventory swagger positioning problem ⽅ formula
- PowerShell:在代理服务器后面使用 PowerShell
- phpstrom设置函数注释说明
- Global and Chinese markets for industrial X-ray testing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- es使用collapseBuilder去重和只返回某个字段
- Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization
猜你喜欢
微信小程序:全网独家小程序版本独立微信社群人脉
What sparks can applet container technology collide with IOT
Hedhat firewall
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
Yyds dry goods inventory kubernetes management business configuration methods? (08)
PowerShell: use PowerShell behind the proxy server
流批一体在京东的探索与实践
runc hang 导致 Kubernetes 节点 NotReady
Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
PowerShell:在代理服务器后面使用 PowerShell
随机推荐
Five ways to query MySQL field comments!
Restful fast request 2022.2.1 release, support curl import
Main window in QT application
node工程中package.json文件作用是什么?里面的^尖括号和~波浪号是什么意思?
微信小程序:独立后台带分销功能月老办事处交友盲盒
Include rake tasks in Gems - including rake tasks in gems
JS implementation determines whether the point is within the polygon range
Outlook: always prompt for user password
Win:使用组策略启用和禁用 USB 驱动器
流批一體在京東的探索與實踐
增量备份 ?db full
Summary of regularization methods
Express routing, express middleware, using express write interface
Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
19. Delete the penultimate node of the linked list
线上故障突突突?如何紧急诊断、排查与恢复
Pytorch fine tuning (Fortune): hollowed out design or cheating
Hedhat firewall
如何搭建一支搞垮公司的技術團隊?
Comment mettre en place une équipe technique pour détruire l'entreprise?