【发布时间】:2026-02-06 10:50:01
【问题描述】:
我有一个带有Account 列表的Bank 类。银行有一个transfer() 方法将价值从一个账户转移到另一个账户。这个想法是在转移中锁定from 和to 帐户。
为了解决这个问题,我有以下代码(请记住,这是一个非常简单的例子,因为它只是一个例子):
public class Account {
private int mBalance;
public Account() {
mBalance = 0;
}
public void withdraw(int value) {
mBalance -= value;
}
public void deposit(int value) {
mBalance += value;
}
}
public class Bank {
private List<Account> mAccounts;
private int mSlots;
public Bank(int slots) {
mAccounts = new ArrayList<Account>(Collections.nCopies(slots, new Account()));
mSlots = slots;
}
public void transfer(int fromId, int toId, int value) {
synchronized(mAccounts.get(fromId, toId)) {
synchronized(mAccounts.get(toId)) {
mAccounts.get(fromId).withdraw(value);
mAccounts.get(toId).deposit(value);
}
}
}
}
这可行,但不能防止死锁。为了解决这个问题,我们需要将同步更改为以下内容:
synchronized(mAccounts.get(Math.min(fromId, toId))) {
synchronized(mAccounts.get(Math.max(fromId, toId))) {
mAccounts.get(fromId).withdraw(value);
mAccounts.get(toId).deposit(value);
}
}
但是编译器警告我嵌套同步块,我相信这是一件坏事?另外,我不太喜欢最大/最小解决方案(我不是提出这个想法的人),如果可能的话,我想避免这种情况。
如何解决上述两个问题?如果我们可以锁定多个对象,我们将锁定from 和to 帐户,但我们不能这样做(据我所知)。那有什么办法呢?
【问题讨论】:
-
你肯定知道你在做什么。我会忽略编译器。
-
大声笑,我只是在学习进程并发......我不会称之为“知道我在做什么”,我只是了解这个基本示例的工作原理。不过谢谢。
-
完全没有,你很好地识别并解决了死锁问题。
-
我假设方法本身不同步的原因是因为示例过于简化?
-
其实我没有,我得到了帮助 :P 正如我所说,这个想法不是来自我。
标签: java concurrency synchronized