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