【发布时间】:2019-06-01 17:21:18
【问题描述】:
这个模拟的银行账户转账函数如下所示,使用 ReentrantLock.newCondition():
class Bank {
private Lock bankLock = new ReentrantLock();
private Condition sufficientFunds = bankLock.newCondition();
private final double[] accounts;
public Bank(int n, double initialBalance) {
accounts = new double[n];
Arrays.fill(accounts, initialBalance);
}
public void transfer(int from, int to, double amount) throws InterruptedException {
bankLock.lock();
try {
while(accounts[from] < amount) {
sufficientFunds.await();
}
System.out.println(Thread.currentThread());
accounts[from] -= amount;//risky
// What if interrupted here ??????
accounts[to] += amount; //risky
sufficientFunds.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
bankLock.unlock();
}
}
看起来没问题,因为这是线程同步使用条件的常用示例,当线程中断时,锁将始终“解锁”。
但是万一这个线程在中间的地方被打断了
accounts[from] -= amount;//risky
和
accounts[to] += amount; //risky
那么银行总金额就不会是余额了!而且我认为将“帐户”声明为原子数组并不能解决问题。我认为问题在于,我应该在交易中赚“+钱”和“-钱”,要么都成功,要么应该回滚。
那么在java并发库中有什么方便的方法来实现这个“事务”吗?或者这需要一些特殊的设计,如何实现?
非常感谢。
【问题讨论】:
-
非常合理的解释。非常感谢。
标签: java multithreading transactions locking conditional-statements