【发布时间】:2018-07-16 03:12:29
【问题描述】:
以下课程,
// This class should be thread-safe!!!
class BankAccount {
private long balance; // Should it be volatile?
synchronized void deposit(long amount) {
// ...
balance += amount;
}
synchronized void withdraw(long amount) {
// ...
balance -= amount;
}
}
我应该将volatile 添加到balance 字段吗?
【问题讨论】:
标签: java multithreading synchronized volatile