/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final fun ff = new fun();
new Thread(new Runnable() {
@Override
public void run() {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ff.a();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
ff.b();
this.notifyAll();
}
}).start();
}
}
class fun {
public void a() {
for (int i = 0; i < 100; i++)
System.out.println("this is A:" + i);
}
public void b() {
for (int i = 0; i < 100; i++)
System.out.println("this is B:" + i);
}
}
报错
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at test.test$1.run(test.java:18)
at java.lang.Thread.run(Thread.java:680)
Exception in thread "Thread-2" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at test.test$2.run(test.java:38)
at java.lang.Thread.run(Thread.java:680) ------最佳解决方案--------------------