lockdemo.java
来自「Java面向对象编程(随书配套源代码) 阐述了面向对象编程的思想」· Java 代码 · 共 35 行
JAVA
35 行
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 + =
减小字号Ctrl + -
显示快捷键?