下面是一些简单的代码:
class B {final static int x = C.x;}
class C {final static int x = B.x;}
class A {
static {
System.out.println("A static{}");
new Thread() { public void run() { new B(); }}.start();
new Thread() { public void run() { new C(); }}.start();
}
public static void main(String[] args) {
System.out.println("A main");
System.out.println("B.x: " + B.x);
System.out.println("C.x: " + C.x);
}
}
B.x
和 C.x
是根据彼此定义的。我认为这不应该编译,但它编译了。
当我尝试运行它时,它在 main 中卡住:
$ javac *.java && java A
A static{}
A main
为什么?
然而,它在 gcj 中工作正常:
$ gcj --main=A -o a *.java && ./a
A static{}
A main
B.x: 0
C.x: 0
为什么?
此外,如果我摆脱线程,
class B {final static int x = C.x;}
class C {final static int x = B.x;}
class A {
static {
System.out.println("A static{}");
new B();
new C();
}
public static void main(String[] args) {
System.out.println("A main");
System.out.println("B.x: " + B.x);
System.out.println("C.x: " + C.x);
}
}
它在 java 和 gcj 中都工作正常:
$ javac *.java && java A
A static{}
A main
B.x: 0
C.x: 0
$ gcj --main=A -o a *.java && ./a
A static{}
A main
B.x: 0
C.x: 0
所有变量都设置为 0。为什么?这不应该编译失败,因为变量是 static final
并且永远不会分配给任何地方吗?
最佳答案
您正在创建一个死锁条件,这可能会或可能不会成为问题,具体取决于您启动线程的速度。
当您第一次使用一个类时,它会初始化该类并调用静态 block 。它以线程安全的方式执行此操作,并且在完成之前没有其他线程可以访问该类。
你有两个线程,在死锁的情况下,一个有 B 类并想要 C 类,另一个有 C 类并想要 B 类。
由于这非常快,一个线程可以在另一个线程开始之前运行完成,在这种情况下,不会发生死锁。
Shouldn't this fail to compile since the variables are static final and are never assigned anywhere?
您已经分配了值,但是您在初始化时访问了该值,这就是您看到默认值 0 的原因。
关于java - 相互递归定义的静态字段导致程序卡住但不是没有线程而不是 gcj?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17980598/