加载是相对于进程而言的,
启动一个main方法,也就是启动了一个进程,在一个进程中,一个类只会被加载一次
public class TestThread {
/**
* 启动一个进程
* 在这个进程中启动多个线程
* 在每个线程中实例化一个类
* @param args
*/
public static void main(String[] args) {
int i = 0;
while(i++ < 5){
new Thread(() -> new P()).start();
}
}
}
class P{
public static int a = 1;
static{
System.out.println("static block ");
}
public P(){
System.out.println("P.P()");
}
}
查看输出结果, 类P只有在第一次被调用的时候才进行初始化, 在之后的实例时都不在进行初始化
static block
P.P()
P.P()
P.P()
P.P()
P.P()