程序如上,我是跟着毕老师的视频一步步敲出来的。可是为什么还是wait出错。实在搞不懂,我试着放进Eclipse里修改,修改了就是把两个wait()用try和catch捕捉,于是我试着按照提示修改并捕捉,然后运行的时候系统又报错Exception in thread "Thread-0" Exception in thread "Thread-1" java.lang.IllegalM
onitorStateException
at java.lang.Object.notify(Native Method)
at Input.run(InputOutputDemo.java:38)
at java.lang.Thread.run(Thread.java:722)
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:503)
at Output.run(InputOutputDemo.java:57)
at java.lang.Thread.run(Thread.java:722)
public class SwapMessage {
public static void main(String[] args) {
Resource res = new Resource();
Input in = new Input(res);
OutPut out = new OutPut(res);
Thread th1 = new Thread(in);
Thread th2 = new Thread(out);
th1.start();
th2.start();