日期:2014-05-20 浏览次数:20937 次
class Maintest {
public static void main(String args[]) throws Exception {
ThreadDemo thr = new ThreadDemo();
thr.start();
for (int i = 1; i < 1000; i++) {
synchronized (Maintest.class) { //Maintest.class是什么意思,非常不明白
System.out.println("main: " + i);
Maintest.class.notify(); //这个与下边怎么联系?
Maintest.class.wait();
}
}
}
}
class ThreadDemo extends Thread {
public void run() {
for (int i = 1; i < 1000; i++) {
try {
synchronized (Maintest.class) { System.out.println(this.getName() + ":" + i);
Maintest.class.notify();
Maintest.class.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
synchronized (Maintest.class) {
System.out.println("main: " + i);
Maintest.class.notify();
}
------解决方案--------------------
这个例子比较有才。
------解决方案--------------------