标题: 线程之间的交互 [打印本页] 作者: 孙铭泽 时间: 2012-8-22 10:53 标题: 线程之间的交互 public class Bank {
private int sum = 0;
Object obj = new Object();
public synchronized void add(int num) {
sum += num;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"***sum:" + sum);
}
}
public class BankDemoTest {
public static void main(String[] args) {
Person p = new Person();
Thread t1 = new Thread(p,"丈夫");
Thread t2 = new Thread(p,"妻子");