public void transfer(Vector<Double> accounts, int from, int to, int amount){
synchronized (accounts){ //添加一个锁
accounts.set(from,accounts.get(from) - amount);
accounts.set(to,accounts.get(to) + amount);
}
}
每个Java对象都有一个锁。线程可以通过调用同步方法获得锁。还有另一种机制可以获得锁,通过进入一个同步阻塞。当线程进入如下形式的阻塞public class Bank { private Object lock = new Object(); public void transfer(int from,int to,int amount){ synchroni...