123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package J20250731.demo01_cooker_foodie;
- /**
- * @author WanJl
- * @version 1.0
- * @title Foodie
- * @description
- * @create 2025/7/31
- */
- public class Foodie implements Runnable{
- /**
- * 1.判断是否有包子,决定当前线程是否执行
- * 2.如果没有包子,就进入等待状态,如果有包子,就消费包子
- * 3.消费包子后,更新桌子上包子状态,唤醒生产者生产包子
- */
- @Override
- public void run() {
- while (true){
- synchronized (Desk.lock){
- if (Desk.count==0){
- break;
- }else {
- if (Desk.flag){
- //消费...
- System.out.println("消费者"+Thread.currentThread().getName()+"正在吃....包子...");
- Desk.flag=false;
- Desk.lock.notifyAll();
- Desk.count--;
- System.out.println("消费后包子还剩:"+Desk.count);
- }else {
- //等待....
- try {
- Desk.lock.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- }
|