This is the mail archive of the java-discuss@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Deadlock in 'initializeClass'



I'm running gcj on a Linux x86 RH6.0 system, using Posix threads, with the 
latest libgcj from the CVS archive. Apparently there is a deadlock in
java::lang::Class::initializeClass (natClass.cc) whereby threads waiting
for classes to be initialized get hung in the 'wait()' call in Step 2 of
the class initialization. While another thread does the 'notify()' that
the class has been initialized, for some reason the other threads never
wake up.

I don't have a fix for this (yet), but it is very easy to duplicate with
the test program below. In this case some threads get hung waiting for
java.lang.System to be initialized (the first class used in the 'run()'
method).

I'm working on it, but thought it might be good to see if others see
this problem and know of a fix. This bug may not have cropped up before
since most programs don't start up a bunch of threads before initializing
most of the classes they need.

Any ideas?

Thanks,
Matt Welsh, mdw@cs.berkeley.edu
UC Berkeley

--

import java.lang.*;

public class TestT extends Thread {

  int threadnum;

  public TestT(int tnum) {
    this.threadnum = tnum;
  }

  public void go(int i) {
    Thread foo = new Thread(this, new String("thread " + i));
    foo.start();
  }

  public void run() {
    int j=0, k=1;

    /* The thread hang occurs during the initialization of java.lang.System
     * below:
     */
    System.err.println("Thread " + Thread.currentThread().getName() + " going");  
    try { Thread.sleep(1000); } catch (Exception e) {} 

    for (;;) {
      try {
        System.err.println(Thread.currentThread().getName() + ": sleeping");
        Thread.sleep(3000);
      } catch (Exception e) {
        System.out.println("Thread sleep interrupted! "+e);
      }
    }
  }

  public static void main(String args[]) {
    TestT a1 = new TestT(1);
    TestT a2 = new TestT(2);
    TestT a3 = new TestT(3);
    TestT a4 = new TestT(4);
    TestT a5 = new TestT(5);

    /* Start all the threads at once. If you stagger them (by calling 
     * Thread.sleep() after each start() call) then you won't see the
     * deadlock.
     */
    a1.start();
    a2.start();
    a3.start();
    a4.start();
    a5.start();

    while (true) { try { Thread.sleep(1000); } catch (Exception e) {} }
  }
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]