From 085eeb33724fac36056725416d9810fa6d05a04e Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Thu, 6 Aug 2026 15:02:52 +0800 Subject: [PATCH] =?UTF-8?q?s-day06-=E7=BA=BF=E7=A8=8B=E7=AD=89=E5=BE=85?= =?UTF-8?q?=E5=94=A4=E9=86=92=E7=9A=84=E6=A1=88=E4=BE=8B(=E7=94=9F?= =?UTF-8?q?=E4=BA=A7=E8=80=85=E4=B8=8E=E6=B6=88=E8=B4=B9=E8=80=85)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/inmind/wait_notify08/BaoZi.java | 5 +++ .../src/com/inmind/wait_notify08/BaoZiPu.java | 33 +++++++++++++++++++ .../src/com/inmind/wait_notify08/GuKe.java | 30 +++++++++++++++++ .../src/com/inmind/wait_notify08/Test10.java | 16 +++++++++ 4 files changed, 84 insertions(+) create mode 100644 s-day06/src/com/inmind/wait_notify08/BaoZi.java create mode 100644 s-day06/src/com/inmind/wait_notify08/BaoZiPu.java create mode 100644 s-day06/src/com/inmind/wait_notify08/GuKe.java create mode 100644 s-day06/src/com/inmind/wait_notify08/Test10.java diff --git a/s-day06/src/com/inmind/wait_notify08/BaoZi.java b/s-day06/src/com/inmind/wait_notify08/BaoZi.java new file mode 100644 index 0000000..e3acda6 --- /dev/null +++ b/s-day06/src/com/inmind/wait_notify08/BaoZi.java @@ -0,0 +1,5 @@ +package com.inmind.wait_notify08; +//该类的作用记录当前是否有包子(就是用来进行线程间通信的) +public class BaoZi { + boolean flag = false;//没有 +} diff --git a/s-day06/src/com/inmind/wait_notify08/BaoZiPu.java b/s-day06/src/com/inmind/wait_notify08/BaoZiPu.java new file mode 100644 index 0000000..2d1893a --- /dev/null +++ b/s-day06/src/com/inmind/wait_notify08/BaoZiPu.java @@ -0,0 +1,33 @@ +package com.inmind.wait_notify08; + +public class BaoZiPu implements Runnable{ + BaoZi baoZi; + + //构造方法来对baozi属性赋值 + public BaoZiPu(BaoZi baoZi) { + this.baoZi = baoZi; + } + + @Override + public void run() { + //如果有包子就等着,如果没有包子就做一个包子 + while (true) { + //使用同步代码块解决线程安全问题 + synchronized (baoZi){ + if (baoZi.flag) { + //如果有包子就等着 + try { + baoZi.wait();//让当前线程等待,释放锁对象 + } catch (InterruptedException e) { + e.printStackTrace(); + } + }else { + //如果没有包子就做一个包子 + System.out.println("包子铺做了一个包子"); + baoZi.flag = true; + baoZi.notify();//唤醒顾客线程,通知包子做好了 + } + } + } + } +} diff --git a/s-day06/src/com/inmind/wait_notify08/GuKe.java b/s-day06/src/com/inmind/wait_notify08/GuKe.java new file mode 100644 index 0000000..4adbb46 --- /dev/null +++ b/s-day06/src/com/inmind/wait_notify08/GuKe.java @@ -0,0 +1,30 @@ +package com.inmind.wait_notify08; + +public class GuKe implements Runnable{ + BaoZi baoZi; + public GuKe(BaoZi baoZi) { + this.baoZi = baoZi; + } + + + @Override + public void run() { + //如果有包子就吃,如果没有包子就等待 + while (true) { + synchronized (baoZi){ + if (baoZi.flag) { + //如果有包子就吃 + System.out.println("顾客吃了一个包子"); + baoZi.flag = false; + baoZi.notify();//唤醒包子铺,让包子铺继续做包子 + }else { + try { + baoZi.wait();//让当前线程等待,释放锁对象 + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + } +} diff --git a/s-day06/src/com/inmind/wait_notify08/Test10.java b/s-day06/src/com/inmind/wait_notify08/Test10.java new file mode 100644 index 0000000..08f557e --- /dev/null +++ b/s-day06/src/com/inmind/wait_notify08/Test10.java @@ -0,0 +1,16 @@ +package com.inmind.wait_notify08; +//实现等待唤醒案例(生产者与消费者) +public class Test10 { + public static void main(String[] args) { + //1.创建包子对象(用来线程间共享,线程间通讯) + BaoZi baoZi = new BaoZi(); + baoZi.flag = true; + //2.创建2个线程任务对象 + BaoZiPu baoZiPu = new BaoZiPu(baoZi); + GuKe guKe = new GuKe(baoZi); + //3.创建线程,启动线程 + new Thread(baoZiPu).start(); + new Thread(guKe).start(); + + } +}