QCHuangChao 发表于 2015-07-13 11:01

多线程模拟对同一个账户,一个来存款一个来取款

多线程访问共享数据package thread;

/**
* 多线程模拟一个存 一个取,存完通知另一个来取
*/
public class ImitateDepositDrawTheadTest {
   
    public static void main(String[] args) {
         
      final DepositOrWithdrawBusiness business = new DepositOrWithdrawBusiness(1000);
      new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                  business.deposit();
                }
            }
      }).start();
      new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                  business.withdraw();
                }
            }
      }).start();
         
         
    }
}
class DepositOrWithdrawBusiness{
    int balance = 1000;
    public DepositOrWithdrawBusiness(int balance){
      this.balance = balance;
    }
    boolean isWithdraw = false;
    public synchronized void deposit(){
      if(isWithdraw){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
      }
      balance = balance+300;
      System.out.println(Thread.currentThread().getName()+ " deposit $300,now balance:" + balance);
      isWithdraw=true;
      this.notify();
         
    }
    public synchronized void withdraw(){
      if(!isWithdraw){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
      }
      balance = balance-300;
      System.out.println(Thread.currentThread().getName()+ " withdraw $300,now balance:" + balance);
      isWithdraw=false;
      this.notify();
    }
}运行结果:
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
。。。
页: [1]
查看完整版本: 多线程模拟对同一个账户,一个来存款一个来取款