条件对象:线程进去临界区,却发现在某一条件满足之后它才能执行,要使用一个条件对象来管理那些已经获得了一个锁但是却不能做有用工作的线程。
至关重要的是最终需要某个其它的线程调用signalAll方法。当一个线程调用await时,它没有办法重新激活自身。它寄希望于其它的线程,如果没有其它线程来重新激活等待的线程,它就永远不再运行了。这将导致令人不快的死锁(deadlock)现象。如果所有其它的线程被阻塞,最后一个活动线程在解除其它线程的阻塞状态之前就调用了await方法,那么它也被阻塞,没有任何线程可以解除其它线程的阻塞,那么该程序就挂起了。
一、获得条件对象的类
Condition newCondition() 返回一个与该锁相关的条件对象。
二、条件对象的方法
1.void await() 将该线程放到条件的等待集中
2.void signalAll() 解除该条件的等待集中的所有线程的阻塞状态
3、viod signal(0 从该条件的等待集中随机的选择一个线程,解除其阻塞状态。
package com.bing.jia;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class UnsynchBankTest{
public static final int NACCOUNTS = 100;//这家银行有多少个账号
public static final double INITIAL_BALANCE = 1000;//每个账号开始的金额
public static void main(String[] args) {
Bank b = new Bank(NACCOUNTS, INITIAL_BALANCE);
int i;
for(i=0;i<NACCOUNTS;i++){
TransferRunnable r = new TransferRunnable(b, i, INITIAL_BALANCE);
Thread t = new Thread(r);
t.start();
}
}
}
class Bank extends Thread{
private final double[] accounts;//数组的长度为银行账号数,数组的值为银行账号的金额
private Lock bankLock;
private Condition sufficientFunds;
public Bank(int n,double initialBalance){
accounts = new double[n];
for(int i=0;i<accounts.length;i++){
accounts[i] = initialBalance;
}
bankLock = new ReentrantLock();
sufficientFunds = bankLock.newCondition();
}
public void transfer(int from,int to,double amount) throws InterruptedException{
bankLock.lock();
try {
if(accounts[from]<amount){
sufficientFunds.await();//将该线程放在条件的等待集中
//return;
}
System.out.println("Thread.currentThread():"+Thread.currentThread());
accounts[from] -= amount;
System.out.println("转账金额数amount:"+amount+" from:"+from+" to:"+to);
accounts[to] += amount;
System.out.println("getTotalBalance()账户剩余金额:"+getTotalBalance());
sufficientFunds.signalAll();//解除该条件的等待集中的所有线程的阻塞状态
}finally{
bankLock.unlock();
}
}
public double getTotalBalance(){
double sum = 0;
for(double a : accounts){
sum += a;
}
return sum;
}
public int size(){
return accounts.length;
}
}
class TransferRunnable implements Runnable{
private Bank bank;
private int fromAccount;
private double maxAmount;
private int DELAY = 10;
public TransferRunnable(Bank b,int from,double max){
bank = b;
fromAccount = from;
maxAmount = max;
}
@Override
public void run() {
try {
while(true){
int toAccount = (int) (bank.size()*Math.random());//随机选一个要收账的账号Math.random()返回0-1的小数
double amount = maxAmount*Math.random();//转账的金额数,始终不能比账号总额大
bank.transfer(fromAccount, toAccount, amount);
Thread.sleep((int)(DELAY*Math.random()));
Thread.sleep(DELAY);
}
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}
运行结果:
Thread.currentThread():Thread[Thread-84,5,main]
转账金额数amount:34.0716924436284 from:83 to:2
getTotalBalance()账户剩余金额:100000.00000000001
Thread.currentThread():Thread[Thread-33,5,main]
转账金额数amount:959.7838084174576 from:32 to:28
getTotalBalance()账户剩余金额:100000.00000000001
Thread.currentThread():Thread[Thread-79,5,main]
转账金额数amount:983.2836877851468 from:78 to:26
getTotalBalance()账户剩余金额:100000.00000000001