写了一下,不知道对不对🤣,主线程放不下不帖了 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; } }
1 1

相关推荐

待现的未见之事:起码第一句要把自己的优势说出来吧。比如什么xx本27届学生,随时到岗....
点赞 评论 收藏
分享
你背过凌晨4点的八股文么:简历挂了的话会是流程终止,像我一样
点赞 评论 收藏
分享
牛客网
牛客企业服务