Cooker02.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package J20250731.demo01_cooker_foodie;
  2. /**
  3. * @author WanJl
  4. * @version 1.0
  5. * @title Cooker02
  6. * @description
  7. * @create 2025/7/31
  8. */
  9. public class Cooker02 implements Runnable{
  10. /**
  11. * 1.判断是否有包子,决定当前线程是否执行
  12. * 2.如果有包子,就进入等待状态,如果没有包子,继续执行,生产包子
  13. * 3.生产包子之后,更新桌子上包子状态,唤醒消费者消费包子
  14. */
  15. @Override
  16. public void run() {
  17. while (true){
  18. synchronized (Desk.lock){
  19. if (Desk.count==0){
  20. System.out.println("厨师"+Thread.currentThread().getName()+"正在生产包子");
  21. Desk.count+=10;
  22. System.out.println("生产后包子增加到:"+Desk.count);
  23. Desk.flag=true;
  24. Desk.lock.notifyAll();
  25. }else {
  26. try {
  27. Desk.lock.wait();
  28. } catch (InterruptedException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }