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