关于用线程进行存取钱交替

来源:互联网 发布:上班 知乎 编辑:程序博客网 时间:2024/06/05 03:50
/**
 * 取钱,存钱。
 * 必须是存一个,取一个,存取相互
 * @author suruidong
 *
 */
public class DrawTest1 {
    public static void main(String[] args) {
        /**
         * 取钱100次
         * 存钱,开了三个线程,所以是300次
         * ,因为是交换存取,所以最后线程会处于等待状态
         */
        Accountt at=new Accountt("123456",0);
        new DrawThreadd("取钱",at,800).start();
        new DepoistThread("存钱1",at,800).start();
        new DepoistThread("存钱2",at,800).start();
        new DepoistThread("存钱3",at,800).start();
    }
}
class DrawThreadd extends Thread{//定义一个取钱的线程
    private Accountt account;
    private double drawAmount;
    public DrawThreadd(String name,Accountt account, double drawAmount) {
        super(name);
        this.account = account;
        this.drawAmount = drawAmount;
    }
    public void run(){
        for(int i=0;i<100;i++){
            account.draw(drawAmount);
        }
    }
}
class DepoistThread extends Thread{//定义一个存钱的线程。
    private Accountt account;
    private double depositAmount;
    public DepoistThread(String name,Accountt account, double depositAmount){
        super(name);
        this.account = account;
        this.depositAmount = depositAmount;
    }
    public void run(){        
            for(int i=0;i<100;i++){
                account.deposit(depositAmount);
    }
}
}
class Accountt{//定义一个账户。里面是账号和资金,因为资金多少不能随便改动。所以提供了两个方法。一个取钱,一个存钱,
    private String accountNo;
    private double balance;
    private boolean flag=false;//因为要做存取的动作,所以定义了一个标记,来规定存取的交替动作
    public Accountt(String accountNo, double balance) {
        super();
        this.accountNo = accountNo;
        this.balance = balance;
    }
    public String getAccountNo() {
        return accountNo;
    }
    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }
    public double getBalance() {
        return balance;
    }
    /**
     * 定义一个同步取钱的方法
     * 当标记为false表示没有钱,在此需要等待
     * @param drawAmount
     */
    public synchronized void draw(double drawAmount){
        try {
            if(!flag){            
                wait();            
            }else{
                System.out.println(Thread.currentThread().getName()+"取钱"+drawAmount);
                balance-=drawAmount;
                System.out.println("余额为:"+getBalance());
                flag=false;
                notifyAll();//唤醒全部线程
                }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 定义一个同步的存钱方法
     * 当标记为false为表示有钱。在此需要等待
     * @param depositAmount
     */
    public synchronized void deposit(double depositAmount){
        try {
            if(flag){            
                wait();
            }else{
                System.out.println(Thread.currentThread().getName()+"存钱"+depositAmount);
                balance+=depositAmount;
                System.out.println("余额:"+getBalance());
                flag=true;
                notifyAll();//唤醒全部线程
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((accountNo == null) ? 0 : accountNo.hashCode());
        long temp;
        temp = Double.doubleToLongBits(balance);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }//直接eclipse的快捷方式创建equal和hashCode方法
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Accountt other = (Accountt) obj;
        if (accountNo == null) {
            if (other.accountNo != null)
                return false;
        } else if (!accountNo.equals(other.accountNo))
            return false;
        if (Double.doubleToLongBits(balance) != Double
                .doubleToLongBits(other.balance))
            return false;
        return true;
    }
}    
    

0 0