JavaMemoryModel: Java Class initialization can deadlock?

From: Bill Pugh (pugh@cs.umd.edu)
Date: Tue Mar 18 2003 - 16:06:43 EST


I believe that according to section 2.17.5 of the JVMS, java class
initialization can deadlock.

Consider:

class A {
   static int x = 1;
   static {
     B.y = 1;
     }
   }

class B {
   static int y = 2;
   static {
     A.x = 2;
     }
   }

If two separate threads tried to initialize classes A and B and the
same time, deadlock could occur.

Here is the sequence of events:

T1 executes steps 1 and 6 of 2.17.5 on class A. Class A is now marked
as InProgress, and unlocked.

T2 executes steps 1 and 6 of 2.17.5 on class B. Class B is now marked
as InProgress, and unlocked.

T1 executes step 8 of 2.17.5 on class A. This involves an active use of B,
so T1 tries to initialize B.

T1 executes step 1 on class B. Since B is marked as InProgress, T1 then
executes step 2, by waiting on B.

T2 executes step 8 of 2.17.5 on class B. This involves an active use of A,
so T2 tries to initialize B.

T2 executes step 1 on class A. Since A is marked as InProgress, T2 then
executes step 2, by waiting on A.

Deadlock.

I've been able to get this behavior to occur very consistently by
putting a two-way barrier in the static initializers for class A and
B.

Is this something we want to fix?

Note that circular initialization does occur. From the 1.4.2 b18 build:
IC: Initialization circularity between
com.sun.jndi.ldap.LdapPoolManager and com.sun.jndi.ldap.pool.Pool
IC: Initialization circularity between
sun.awt.datatransfer.DataTransferer and
sun.awt.datatransfer.DataTransferer$CharsetComparator
IC: Initialization circularity between
sun.awt.datatransfer.DataTransferer and
sun.awt.datatransfer.DataTransferer$DataFlavorComparator
IC: Initialization circularity between
sun.awt.datatransfer.DataTransferer$CharsetComparator and
sun.awt.datatransfer.DataTransferer$DataFlavorComparator
IC: Initialization circularity between
sun.nio.cs.ThreadLocalCoders and sun.nio.cs.ThreadLocalCoders$1
IC: Initialization circularity between
sun.nio.cs.ThreadLocalCoders and sun.nio.cs.ThreadLocalCoders$2
IC: Initialization circularity between
sun.nio.cs.ThreadLocalCoders$1 and sun.nio.cs.ThreadLocalCoders$2

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:42 EDT