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不能直接写啊。。求正确写法。
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){
}
}
}
}
直接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();
}
}
}