⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lockdemo.java

📁 Java面向对象编程(随书配套源代码) 阐述了面向对象编程的思想
💻 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 + -