不知道楼主要实现什么功能你在while ( singleton == null ) {
    Toolkit.class.wait(100);
    initToolkit();
}不就可以了吗?

解决方案 »

  1.   

    我改了一下
    class NativeCollector extends Thread {
    static int i;
    NativeCollector () {
    super ( "awt-native" );
    } public void run() {
        Toolkit.initToolkit();
    }
    }
    class waitThread extends Thread {
    static int i;
    waitThread () {
    super ( "awt-native1" );
    } public  void run() {
        try {
    synchronized ( Toolkit.class) {
    while ( Toolkit.singleton == null ) {
    System.out.println("wait");
    Toolkit.class.wait();
    }
    }
    }
    catch ( Exception x ) {
    x.printStackTrace();
    }
    }
    }public class Toolkit {
    static Toolkit singleton;
    static NativeCollector collectorThread; static void test() {
    //static {
    collectorThread = new NativeCollector();
        collectorThread.start();
    try {
    synchronized ( Toolkit.class) {
    while ( singleton == null ) {
    System.out.println("wait");
    Toolkit.class.wait();
    }
    }
    }
    catch ( Exception x ) {
    x.printStackTrace();
    }

    } Toolkit () {
    } static synchronized void initToolkit() {
        singleton = new Toolkit();
    System.out.println("notifyAll");
        Toolkit.class.notifyAll();
    } public static void main(String [] args) {
    waitThread waitT = new waitThread();
        waitT.start();
    NativeCollector collectorThread = new NativeCollector();
        collectorThread.start(); }
    }
      

  2.   

    但是上面我写的,就改变了你的题意了请看下面的,应该是你想要的
    class NativeCollector extends Thread {
    static int i;
    NativeCollector () {
    super ( "awt-native" );
    } public void run() {
        Toolkit.initToolkit();
    }
    }public class Toolkit {
    static Toolkit singleton;
    static NativeCollector collectorThread; static {
    class inner extends Thread
    {
    public  void run() {
    try {
    synchronized ( Toolkit.class) {
    while ( Toolkit.singleton == null ) {
    System.out.println("wait");
    Toolkit.class.wait();
    }
    }
    }
    catch ( Exception x ) {
    x.printStackTrace();
    }
    }
    }
    inner w=new inner();
    w.start();
    collectorThread = new NativeCollector();
        collectorThread.start();
    } Toolkit () {
    } static synchronized void initToolkit() {
        singleton = new Toolkit();
    System.out.println("notifyAll");
        Toolkit.class.notifyAll();
    } public static void main(String [] args) {
    }
    }
      

  3.   

    主要是利用了内部类你的错误之处在于,wait必须在线程中,而你没有将他放入线程不过20分,也太少了请把它提交给faq吧。这样俺也能加点信誉分