📄 lockdemo.java
字号:
package chapter10;
public class LockDemo implements Runnable
{
protected CustomerAccount fromAccount;
protected CustomerAccount toAccount;
protected int transferCount;
public LockDemo(CustomerAccount fromacct,CustomerAccount toacct, int transfers)
{
fromAccount = fromacct;
toAccount = toacct;
transferCount = transfers;
}
public void run()
{
double balance;
double transferAmount;
for (int i = 0 ; i < transferCount; i++)
{
synchronized (fromAccount)
{
balance = fromAccount.getBalance();
transferAmount = (int)(balance * Math.random());
balance -= transferAmount;
fromAccount.setBalance(balance);
synchronized (toAccount)
{
balance = toAccount.getBalance();
balance += transferAmount;
toAccount.setBalance(balance);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -