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,"妻子");
t1.start();
t2.start();
}
}
这种线程之间的对象怎么保证是一个呢 ,他们之间的锁还可以是别的吗
|