日期:2014-05-20 浏览次数:20922 次
class ThreadB extends Thread {
int total;
public void run() {
/*try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
synchronized (this) {
for (int i = 0; i < 101; i++) {
total += i;
}
notify();
}
}
}
public class ThreadA {
public static void main(String[] args) throws Exception {
ThreadB b = new ThreadB();
b.start();
Thread.sleep(100);
synchronized (b) {// 这里加上同步以后,下面的b.wait()注释掉,b.total输出0,如果加上输出5050 //{
try {
System.out.println("等待B");
b.wait();
System.out.println("shdhihhi");
} catch (Exception e) {
System.out.println(e);
}
System.out.println(b.total);
}
}
}