java多线程--银行存款取款

来源:互联网 发布:2017淘宝店女装排行榜 编辑:程序博客网 时间:2024/05/01 05:12

    tom和kaite共有一个银行账户,初始账目为 1000,某一时刻银行操作员发现tom和kate同时对账户操作,分别取款和存款,各操作10次,每次金额为1000.用java多线程的思想来描述。


    对题目进行分析,两人共有一个银行账户,为Account类;两人同时操作,涉及会引起数据不一致,采用同步操作。


package com.accout;

public class Account {

    private float saving = 1000;
    
    private float varient = 0;
    
    //每次存款后,将操作人存款额打印出来
    public synchronized void add(String name , float varient){
        this.varient = varient;
        saving += this.varient;
        System.out.println(name + " saving " + varient + " to the account ");
        System.out.println("the current account has " + saving );
    }
    
    public synchronized void withdraw(String name , float varient){
        this.varient = varient;
        saving -= this.varient;
        System.out.println(name + " withdraw " + varient + " from the account ");
        System.out.println("the current account has " + saving );
    }
}


    同时对账户进行操作,创建操作类Operation,操作类有存款和取款方法,分别开启线程,调用Account类的方法;

package com.accout;

public class Operation {

    Account account = new Account();
    
    public void addOperation (final String name ,final float varient){
        
        //加载线程
       new Runnable(){

        @Override
        public void run() {
            try{
                Thread.sleep(1000);
            }catch(Exception e){
                e.printStackTrace();
            }
            //存钱
            getAccount().add(name, varient);
        }
           
       }.run();     //别忘了加run()
    }
    
    public void withdrawOperation (final String name ,final float varient){
        
        //加载线程
           new Runnable(){
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //取钱
                getAccount().withdraw(name, varient);
            }
            
           }.run();
        }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}



    客户端调用Client类

package com.accout;

public class Client{

    public static void main(String[]args){
        Operation oper = new Operation();
        for(int i=0 ; i< 10 ; i ++){
            oper.addOperation("tom", 1000);
            oper.withdrawOperation("kaite", 1000);
        }
    }
}


 结果显示:

tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0




原创粉丝点击