/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bank;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* @author Administrator
*/
public class Bank
{
/**
9. * Constructs the bank.
10. * @param n the number of accounts
11. * @param initialBalance the initial balance for each account
12. */
public Bank(int n, double initialBalance)
{
accounts = new double[n];
for (int i = 0; i < n; i++)
accounts[i] = initialBalance;
bankLock = new ReentrantLock();
sufficientFunds = bankLock.newCondition();
}
public void transfer(int from, int to, double amount) throws InterruptedException
{
bankLock.lock();
try{
while (accounts[from] < amount)
sufficientFunds.await();
System.out.print(Thread.currentThread());
accounts[from] -= amount;
System.out.printf(" %10.2f from %d to %d", amount, from, to);
accounts[to] += amount;
System.out.printf(" Total Balance: %10.2f%n", getTotalBalance());
sufficientFunds.signalAll();
}finally
{
bankLock.unlock();
}
}
/**
37. * Gets the sum of all account balances.
38. * @return the total balance
39. */
public double getTotalBalance()
{
double sum = 0;
for (double a : accounts)
sum += a;
return sum;
}
/**
51. * Gets the number of accounts in the bank.
52. * @return the number of accounts
53. */
public int size()
{
return accounts.length;
}
private final double[] accounts;
private Lock bankLock;
private Condition sufficientFunds;
}
Bank.zip_Table_条件查询
版权申诉
156 浏览量
2022-09-24
02:30:31
上传
评论
收藏 17KB ZIP 举报
小波思基
- 粉丝: 73
- 资源: 1万+
最新资源
- bj计算机思维citel
- 课程设计作业-SpringBoot企业产品仓库管理系统(文档 + 源码 + 数据库文件)
- python相关知识点说明
- 基于 STM32 实现交通灯控制程序源码+详细文档+全部资料(优秀项目).zip
- linux简介及基础教程及特点阐述.txt
- 数据结构课程设计课作业资料
- java简介及基础教程及特点阐述.txt
- 一个简单的数据分析脚本示例,它使用Python和pandas库来读取CSV文件、执行一些基本的数据清洗和转换,并进行简单的可视化
- AD7124+PT100 基于STM32f103的代码 检测多路温度(100~-200)并输出
- CHROME webdriver Version: 125.0.6422.78 win64
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈