for (int i = 0; i < 100; i++) {
            Thread t = new Thread();
            t.start();
            try {
                t.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }Exception in thread "main" java.lang.IllegalMonitorStateException不能直接写啊。。求正确写法。

解决方案 »

  1.   


    public class ThreadTest implements Runnable{
    public static void main(String[] args){
    ThreadTest test=new ThreadTest();
    for(int i=0;i<100;i++){
    new Thread(test).start();
    }
    } private Object lock=new Object();
    public void run(){
    synchronized(lock){
    try{
    lock.wait();
    }
    catch(InterruptedException e){
    }
    }
    }
    }
      

  2.   

    你执行wait的时候,你start的线程可能已经执行结束了
      

  3.   

    你查下API看看这个异常,我看了,但是不明白
      

  4.   

    Object.wait()只有在当前线程中才能使用,否则可能会报运行时错误java.lang.IllegalMonitorStateException: current thread not owner.呵呵 不知道你的目的是什么?
      

  5.   

    java.lang.IllegalMonitorStateException 是因为  wait不是处于synchronized 中
      

  6.   


    直接wait不用synchronized也不行,为什么?(如下,还是那错误)public class ThreadTest1 implements Runnable{    public void run() {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }    public static void main(String[] args) {
            for (int i = 0; i < 100; i++) {
                new Thread(new ThreadTest1()).start();
            }
        }
    }
      

  7.   

    是因为 wait必需处于synchronized 中,参考1楼代码