Loading of classes and static blocks are implicitly synchronized This means you cannot access anything in a class in another thread while it is being initialised. In this case the initialisation is waiting for a thread which is using A.i
. In other words, it is waiting for the first thread to finish the static block.
Note: it doesn't use a normal lock and the thread claims to be in a Runnable state even though it is deadlocked.
2013-06-21 11:20:40
Full thread dump Java HotSpot(TM) 64-Bit Server VM (23.21-b01 mixed mode):
"Thread-0" prio=6 tid=0x000000000d55d000 nid=0x3cc4 in Object.wait() [0x000000000dbdf000]
java.lang.Thread.State: RUNNABLE
at Main$1.run(Main.java:14) <- where A.i is referenced.
"main" prio=6 tid=0x00000000022df000 nid=0x3284 in Object.wait() [0x000000000257e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d5610448> (a Main$1)
at java.lang.Thread.join(Thread.java:1258)
- locked <0x00000007d5610448> (a Main$1)
at java.lang.Thread.join(Thread.java:1332)
at Main.(Main.java:19)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)