请看以下代码:public class TestThread {
public static Object obj = new Object();
public static int i = 0;
public static void main(String[] args) {
Runnable r1 = new Runner1();
Thread t1 = new Thread(r1);
t1.start();
try {
Thread.sleep(1);
} catch(InterruptedException ie) {} synchronized(TestThread.obj) {
TestThread.i++;
try {
Thread.sleep(1);
} catch(InterruptedException ie) {}
System.out.println(Thread.currentThread() + "@ i = " + TestThread.i);
}
}
}class Runner1 implements Runnable {
public Object oo = new Object();
public void run() {
// 这里出错了! 不能为 TestThread.obj!
// 换成 this 就正确, 为什么呢?
synchronized(TestThread.obj) {
TestThread.i++;
try {
this.wait();
} catch(InterruptedException ie) {}
System.out.println(Thread.currentThread() + "@ i = " + TestThread.i);
}
}
}以上代码出现的错误提示信息为:
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
Thread[main,5,main]@ i = 3
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(Object.java:485)
        at Runner1.run(TestThread.java:33)
        at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(Object.java:485)
        at Runner1.run(TestThread.java:33)
        at java.lang.Thread.run(Unknown Source)这个错误的原因是 synchronized(TestThread.obj) 为什么用了wait()方法的 synchronized 里的锁不能为 TestThread.obj 呢?
请高手详细解释, 谢谢!