通过wait,notify等实现线程通讯

通过wait,notify等实现线程通讯

package org.thread.demo.threadcommunication;
/**
 * 银行账号类
 *
 * */
public class Account {
          //账号
          private String account;
          //密码
          private String password;
          //余额
          private volatile  double amount;
          
          //定义标识 是不是有钱
          private boolean flag = true;
          /**
           * 取钱
           * */
          public synchronized  void suspend(double money) {
                   try {
                             Thread.sleep(1000);
                             if(flag) {
                                      if(this.getAmount()>=money) {
                                                this.setAmount(this.getAmount() - money);
                                                System.out.println(Thread.currentThread().getName()+"取款成功");
                                                if(this.getAmount()<=0) {
                                                          this.flag = false;
                                                }
                                      }else {
                                                System.out.println(Thread.currentThread().getName()+"余额不足,取款失败");
                                                //通知其他线程打钱  小头爸爸  二叔  王叔
                                                this.notifyAll();
                                                //自己的线程进行等待
                                                this.wait();
                                      }
                             }else {
                                      //通知其他线程打钱  小头爸爸  二叔  王叔
                                      this.notifyAll();
                                      //自己的线程进行等待
                                      this.wait();
                             }
                   } catch (Exception e) {
                             e.printStackTrace();
                   }
          }
          
          /**
           * 存钱
           * */
          public synchronized  void susume(double money) {
                   try {
                             Thread.sleep(1000);
                             if(flag) {
                                      //卡里有钱小明取钱
                                      this.notifyAll();
                                      //自己等待
                                      this.wait();
                             }else {
                                      //卡里没钱 存款
                                      this.setAmount(money+this.getAmount());
                                      flag=true;
                                      System.out.println(Thread.currentThread().getName()+"存款成功");
                                      this.notifyAll();
                                      //自己等待
                                      this.wait();
                             }
                   } catch (Exception e) {
                             // TODO Auto-generated catch block
                             e.printStackTrace();
                   }
                   
          }
          
          public String getAccount() {
                   return account;
          }
          public void setAccount(String account) {
                   this.account = account;
          }
          public String getPassword() {
                   return password;
          }
          public void setPassword(String password) {
                   this.password = password;
          }
          public double getAmount() {
                   return amount;
          }
          public void setAmount(double amount) {
                   this.amount = amount;
          }
          @Override
          public String toString() {
                   return "Account [account=" + account + ", password=" + password + ", amount=" + amount + "]";
          }
}
package org.thread.demo.threadcommunication;
/**
 * 线程通讯
 * 当卡了余额不足时候,通知其他线程存钱
 * 当线程存款完成后,通知其他线程去取现
 * wait  notify线程通讯
 * */
public class Test {
          public static void main(String[] args) {
                   //创建账户
                   Account account = new Account();
                   account.setAccount("1");
                   account.setPassword("1");
                   account.setAmount(10000);
                   
                   //模拟三个线程 一个取钱 三个存钱
                   SuspendThread suspendThread = new SuspendThread(account,10000);
                   suspendThread.setName("大头儿子");
                   suspendThread.start();
                   
                   SusumeThread susumeThread0 = new SusumeThread(account,10000);
                   susumeThread0.setName("小头爸爸");
                   SusumeThread susumeThread1 = new SusumeThread(account,10000);
                   susumeThread1.setName("二叔");
                   SusumeThread susumeThread2 = new SusumeThread(account,10000);
                   susumeThread2.setName("老王");
                   susumeThread1.start();
                   susumeThread0.start();
                   susumeThread2.start();
                   
          }
}
package org.thread.demo.threadcommunication;
/**
 *
 * 取现*/
public class SuspendThread  extends Thread{
          ///定义成员变量,同步到内存中
          private Account account;
          private double money;
          public SuspendThread(Account account,double money) {
                   this.account = account;
                   this.money = money;
          }
          
          //不断的取钱
          @Override
          public void run() {
                   while(true) {
                             account.suspend(money);
                   }
          }
}
package org.thread.demo.threadcommunication;
/**
 * 存钱
 * */
public class SusumeThread  extends Thread{
          ///定义成员变量,同步到内存中
                   private Account account;
                   private double money;
                   
                   public SusumeThread(Account account,double money) {
                             this.account = account;
                             this.money = money;
                   }
                   //不断的存钱
                   @Override
                   public void run() {
                             while(true) {
                                      account.susume(money);;
                             }
                   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值