JDK-4151836 : Race condition in class initialization
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: unknown
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.6
  • CPU: sparc
  • Submitted: 1998-06-24
  • Updated: 1998-07-29
  • Resolved: 1998-07-29
Related Reports
Duplicate :  
Description
System is a Ultra 30/256MB ram.

I'm using the SunSoft JDK1.1.5 release with 
native threads but the problem also occurs
using the JavaSoft JDK1.1.6 with native
threads.

If you run the sample application it crashes
with:

java.lang.NullPointerException
at Fred$1.construct(Fred.java:13)
at Fred$2.run(Fred.java:31)
at java.lang.Thread.run(Thread.java)


Like I said, bizarre.


public class Fred  {
   
   public String s = "hello";
  
   public void doIt() {
      Worker w = new Worker();
      
      Worker worker = new Worker() {
         public void construct() {
            System.out.println("This Fails");
            s = "Hello There";
         }
      };
   }
   
   public static void main(String[] args) {
      Fred f = new Fred();
      f.doIt();
   }
    
   class Worker {
      public void construct() {
         System.out.println("This Works");
         s = "Hello There";
      }
      public Worker() {
         Runnable doConstruct = new Runnable() {
            public void run() {
               construct();
            }
         };
         Thread thread = new Thread(doConstruct);
         thread.start();
      }
   }
} 

peter.castle@rsa 1998-07-06
peter.castle@rsa 1998-07-21

Comments
SUGGESTED FIX no fix
11-06-2004

PUBLIC COMMENTS Bizarre native thread behavior System is a Ultra 30/256MB ram. I'm using the SunSoft JDK1.1.5 release with native threads but the problem also occurs using the JavaSoft JDK1.1.6 with native threads. If you run the sample application it crashes with: java.lang.NullPointerException at Fred$1.construct(Fred.java:13) at Fred$2.run(Fred.java:31) at java.lang.Thread.run(Thread.java) public class Fred { public String s = "hello"; public void doIt() { Worker w = new Worker(); Worker worker = new Worker() { public void construct() { System.out.println("This Fails"); s = "Hello There"; } }; } public static void main(String[] args) { Fred f = new Fred(); f.doIt(); } class Worker { public void construct() { System.out.println("This Works"); s = "Hello There"; } public Worker() { Runnable doConstruct = new Runnable() { public void run() { construct(); } }; Thread thread = new Thread(doConstruct); thread.start(); } } }
10-06-2004

WORK AROUND I suspect this problem does *not* occur on slower systems because I can overcome the problem if I put a Thread.sleep(5) before the s = "Hello There" in the construct method -- See the workaround for 4030374. david.stoutamire@Eng 1998-07-29
29-07-1998

EVALUATION Very reproducible. I think this is a race condition between two threads trying to initialize a class at once. I can not reproduce this in 1.2. anand.palaniswamy@Eng 1998-07-23 This is reproducible with both 1.1.6 and 1.2, on *native* threads. This is likely a known javac problem that does not initialize the *this* pointer to outer classes properly. sheng.liang@Eng 1998-07-28 I also reproduced it under native threads. The root cause is 4030374, but this bug is interesting because that problem can also manifest as a race condition when there are multiple threads. Threads are working per se. david.stoutamire@Eng 1998-07-29
29-07-1998