java clinit 死锁

public class StaticInit {
	public static void main(String[] arg) {
		Thread th1 = new Thread(new Runnable() {
			public void run() {
				System.out.println(B.bStr);
			}
		}, "call-bStr");

		Thread th2 = new Thread(new Runnable() {
			public void run() {
				System.out.println(C.cStr);
			}
		}, "call-cStr");
		th1.start();
		th2.start();
	}
}

class B {
	static {
		try {
			System.out.println("Binit start");
			Thread.sleep(1000);
		} catch (InterruptedException ex) {
		}
		System.out.println("b->cStr " + C.cStr);
	}
	static String bStr = "bStr";
}

class C {
	static {
		try {
			System.out.println("Cinit start");
			Thread.sleep(1000);
		} catch (InterruptedException ex) {
		}
		System.out.println("c->b.bStr " + B.bStr);
	}
	static String cStr = "cStr";
}

 

"call-cStr" prio=6 tid=0x0257cc00 nid=0x18a4 in Object.wait() [0x04c3f000]
   java.lang.Thread.State: RUNNABLE
    at C.<clinit>(StaticInit.java:40)
    at StaticInit$2.run(StaticInit.java:13)
    at java.lang.Thread.run(Thread.java:662)

"call-bStr" prio=6 tid=0x0257a400 nid=0x1ddc in Object.wait() [0x04baf000]
   java.lang.Thread.State: RUNNABLE
    at B.<clinit>(StaticInit.java:28)
    at StaticInit$1.run(StaticInit.java:7)
    at java.lang.Thread.run(Thread.java:662)

 

你可能感兴趣的:(java)