当前位置:网站首页>clinit方法
clinit方法
2022-07-30 05:45:00 【zpv2jdfc】
- 当类有字段赋值操作或者静态代码块时,编译后会为这个类生成clinit方法来执行这些语句。
- clinit方法是被加锁的,同于一时刻只能有一个线程执行某个类的clinit方法。
static class A{
static{
//clinit方法
if(true){
System.out.println(Thread.currentThread() + "init Class A");
while (true) {
}
}
}
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread()+" start");
A a = new A();
System.out.println(Thread.currentThread()+" finish");
}
};
//两个线程要加载同一个类
new Thread(runnable).start();
new Thread(runnable).start();
}
两个线程要都要新建A的实例,在类加载的时候,一个线程卡在了静态方法里的死循环中,另一个线程进不去clinit方法。
输出结果:
Thread[Thread-0,5,main] start
Thread[Thread-1,5,main] start
Thread[Thread-0,5,main]init Class A
边栏推荐
猜你喜欢

常用损失函数(一):Focal Loss

十二、Kotlin进阶学习:一、Lambda 表达式;二、高阶函数;

Trust anchor for certification path not found. Exception solution.

海量遥感数据处理与GEE云计算技术应用【基础、进阶】

Target detection, object classification and semantic segmentation of UAV remote sensing images based on PyTorch deep learning

QT每周技巧(3)~~~~~~~~~串口添加

Rsync实现Win系统间的文件夹或数据同步

探究make_shared效率

Configure MMdetection environment and train

边境的悍匪—Kaggle—泰坦尼克号生还预测详细教程
随机推荐
工厂模式(Swift 实现)
十二、Kotlin进阶学习:一、Lambda 表达式;二、高阶函数;
R language application in the field of ecological environment
R - GIS: how to use R language implementation of GIS geospatial analysis and model prediction
Flink-stream/batch/OLAP integrated to get Flink engine
树莓派OpenCV+OpenCV-contrib
vs编译boost库脚本
基于OpenCV的双目重建
2021-09-16 集成学习上--task1机器学习数学基础
写在公众号之前——QT,ARM,DSP,单片机,电力电子与传动!
边境的悍匪—机器学习实战:第十二章 使用TensorFlow自定义模型和训练
HSPF 模型应用
1.03 original Acegi security mechanism
Based on R language geographic weighted regression, principal component analysis, discriminant analysis and other spatial heterogeneity data analysis
点云统计滤波理解
Arthas command parsing (watch/tt/sc)
QT串口动态实时显示大量数据波形曲线(四)========“界面的美化与处理”
边境的悍匪—机器学习实战:第二章 端到端的机器学习项目
十九、Kotlin进阶学习:1、管道数据的收和发;2、管道的关闭;3、生产者和消费者;4、管道的缓存区;
目标检测中的知识蒸馏方法