美文网首页
Java多线程(二)-线程同步

Java多线程(二)-线程同步

作者: 沧海一粟谦 | 来源:发表于2018-04-07 19:53 被阅读31次
    The Pursuit of Happyness
    public class Bank extends Thread {
     /**
      * 静态字段:用于表示储存
      */
     public static final int DEAL_SAVING = 0;
     /**
      * 静态字段:用于表示提取
      */
     public static final int DEAL_FETCH = 1;
     private int buy = Bank.DEAL_FETCH; // 默认使取款
     private int count = 0;
     private MyAccount myAccount = null; // 我的帐号
     /**
      * 构造这个银行交易大厅
      * @param name 这个交易大厅的名称
      * @param myAccounts 我的银行帐号
      * @param buy 行为,参考字段:DEAL_SAVING或DEAL_FETCH
      * @param count 钱的数量
      */
     public Bank(String name, MyAccount myAccounts, int buy, int count){
         super(name);
         this.myAccount = myAccounts;
         this.buy = buy;
         this.count = count;
         this.start();
     }
     public void run(){
         int $count = 0;
         if(buy == Bank.DEAL_SAVING){ // 如果是存款业务
             $count = myAccount.save(count);
         }else if(buy == Bank.DEAL_FETCH){ // 如果是取款业务
             $count = myAccount.fetch(count);
         }
      
        System.out.println("\n" + this.getName() + "   " + (buy == Bank.DEAL_SAVING ? "存款": "取款") + "   金额:" + count + "    结余:" + $count);
     }
    }
    
    public class MyAccount {
        private int balance = 1000;
        public MyAccount(){
        
        }   
        public int getBalance(){
            return balance;
        }
        
        public synchronized int save(int input) {
                int $count = getBalance();
                $count = $count + input;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                balance = $count;
                return getBalance();
        }
        
        public synchronized int fetch(int output){
                int $count = getBalance();
                $count = $count - output;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                balance = $count;
                return getBalance();
        }
    }
    
    public class BankDemo {
    
        public static void main(String[] args) {
            
              MyAccount myAccounts = new MyAccount();
              System.out.println("账户初始余额是:"+myAccounts.getBalance());
              new Bank("航空港支行", myAccounts, Bank.DEAL_SAVING, 800);
              new Bank("高新区支行", myAccounts, Bank.DEAL_SAVING, 1300);
              new Bank("春熙路支行", myAccounts, Bank.DEAL_FETCH, 200);
              new Bank("火车站支行", myAccounts, Bank.DEAL_FETCH, 400);
              new Bank("九眼桥支行", myAccounts, Bank.DEAL_SAVING, 100);
              new Bank("西门车站支行", myAccounts, Bank.DEAL_FETCH, 700);
        }
    
    }
    
    img.png

    相关文章

      网友评论

          本文标题:Java多线程(二)-线程同步

          本文链接:https://www.haomeiwen.com/subject/hjtuhftx.html