这个模拟银行账户转账功能如下,使用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并发库中有什么方便的方法来实现这个"事务"吗?或者这需要一些特殊的设计,以及如何实现它?
多谢。
面中断不能随机发生。
如果有人调用Thread.interrupt
它不会立即停止线程。
ThreadInterruptException
将仅从声明它的方法引发。
因此,如果您不从代码中调用任何此类方法,则没有问题。