2019.4.26
class AnotherBank{
private double[] accounts
;
public static final ThreadLocal
<SimpleDateFormat> dataFormat
=
ThreadLocal
.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
public AnotherBank(int n
, double initialBalance
){
accounts
= new double[n
];
Arrays
.fill(accounts
, initialBalance
);
}
public synchronized void transfer(int from
, int to
, int amount
) throws InterruptedException
{
while(accounts
[from
] < amount
)
wait();
accounts
[from
] -= amount
;
accounts
[to
] += amount
;
notifyAll();
}
public synchronized double getTotalBalance(){
double sum
= 0;
for(double a
: accounts
)
sum
+= a
;
return sum
;
}
public int size(){
return accounts
.length
;
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-19360.html