---
title: "线程学习二，notify和wait实现消费者-生产者同步问题"
author: "GreatNeyo"
date: "2010-07-27T02:06:01Z"
tags: ["thread"]
categories: ["编程语言"]
slug: "notify-wait"
draft: false
lastmod: "2010-07-27T02:06:01Z"
id: 721917
url: "https://greatneyo.iteye.com/blog/721917"
---

```java

/*
生产者-消费者的问题，对象有学生，家长，银行，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();
    }
}


```