我正在开发与等待和通知相关的简单代码我创建了两个单独的类,下面是类
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 中,当我从 b 线程对象访问总数时出现编译时间错误,即..
System.out.println("Total is: " + b.totals);
请告诉我如何更正它,以便我可以执行我的代码..!! 提前谢谢...!1