我正在制定相关的等待&简单的代码通知我创建了两个seprate类,下面是类关于等待和通知
class ThreadA {
public static void main(String [] args) {
Thread b = new Thread();
b.start();
synchronized(b) {
try {
System.out.println("Waiting for b to complete...");
b.wait();
} catch (InterruptedException e) {}
//System.out.println("Total is: " + b.totals);
}
}
}
and the other one is ...
class ThreadB extends Thread {
public int totals;
public void run() {
synchronized(this) {
for(int i=0;i<100;i++) {
totals += i;
}
notify();
}
}
}
但类的ThreadA内,我收到complie时间错误,当我我从b线程对象,它是访问的总数..
System.out.println("Total is: " + b.totals);
请告诉我如何纠正它,这样我可以执行我的代码.. !!在此先感谢.. 1
虽然答案已经被发现。未来的问题可以通过包含遇到的实际错误消息来改进。 – 2012-04-19 17:30:12
而且当你发布代码时,请确认标识 – Cratylus 2012-04-19 17:33:15