线程学习二,notify和wait实现消费者-生产者同步问题


/*
生产者-消费者的问题,对象有学生,家长,银行,ATM取款机
 * 实现,学生在学校用钱,钱用光了用银行卡去ATM取款,卡上有钱时把钱取出来消费,卡上没有钱时学生打电话通知家长,家长到银行存钱,
 * 银行同步把存入的钱反映到银行卡上,家长通知学生钱已经打过去了,学生接到家长的通知后,就去ATM机取钱。钱取到后,学生就去消费,
 * 当钱用完时,学生就 通知家长 去存钱。
 * 对象有:学生,家长,银行(包括银行卡、ATM机)
 */

package threadTest;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class ProducerCustomer {
   static class Bank {
        static double money = 0;

        public double getMoney() {
            return money;
        }

        public void setMoney(double money) {
            this.money = money;
        }
    }
    Bank ban = new Bank();//加同步锁的对象
    class Student implements Runnable{
        public void run(){
               
            synchronized(ban){
                 while(true){
                while(ban.getMoney() > 0){
                    try {
                        System.out.println("学生被告知钱已经打了,学生去取钱消费");
                        Thread.sleep(2000);
                        moneyUsering();
                        moneyUserOut();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ProducerCustomer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                while(ban.getMoney()==0){
                    moneyUserOut();
                }
               
            }
            }
        }
        public void moneyUsering(){
            while(ban.getMoney() !=0){
                    System.out.println("卡里还有"+ban.getMoney()+"钱");
                    double newMoney = ban.getMoney()-100;
                    ban.setMoney(newMoney);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ProducerCustomer.class.getName()).log(Level.SEVERE, null, ex);
                }
                }
        }
        public void moneyUserOut(){
            while(ban.getMoney() == 0){
                    System.out.println("学生发现钱花光了,通知家长去打钱"+ban.getMoney());
                    ban.notify();//钱用光时,通知家长线程解锁
                       try {
                           System.out.println("等待家长打钱");
                           Thread.sleep(3000);
                           ban.wait();//当前线程进入阻塞状态
                           System.out.println("--------");
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ProducerCustomer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
    }
    class Parents implements Runnable{
        public void run(){
            synchronized(ban){
                while(true){
                    while(ban.getMoney() == 0){
                        ban.setMoney(500);
                        System.out.println("家长去银行存入了500元钱"+ban.getMoney());

                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(ProducerCustomer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        System.out.println("家长通知学生钱已经存进去了,等待学生通知要钱");
                        ban.notify();//解锁学生线程
                        try {
                            ban.wait();//当前线程进入等待状态
                        } catch (InterruptedException ex) {
                            Logger.getLogger(ProducerCustomer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                   }
                }
                
            }
        }
    }
    
    public static void main(String[] args) {
        ProducerCustomer pc = new ProducerCustomer();
        Student stu = pc.new Student();
        Parents par = pc.new Parents();
        Thread thStu = new Thread(stu);
        Thread thPar = new Thread(par);
        thStu.start();
        thPar.start();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值