写了一下,不知道对不对🤣,主线程放不下不帖了 class Data { // 存款 int money = 0; // 要取的钱 int need = 0; // 判断取钱标志 public boolean flag = false; // 生产者 public synchronized void produce(int num) throws InterruptedException { money += num; System.out.println("已存入" + num + "元" + ",现在有" + money + "元"); this.notify(); this.wait(); return; } // 消费者 public synchronized void consume(int num) throws InterruptedException { need = num; System.out.println("需要" + need + "元"); while (money < need) { this.notify(); this.wait(); } money -= need; System.out.println("取钱成功,还剩" + money + "元"); flag = true; this.notify(); // 唤醒producer线程,否则无法正常结束 return; } }