新闻详情

新闻详情

首页 / 资讯中心 / 详情

等待唤醒案例分析(线程之间的通信)

发布时间:2026/10/30 4:36:20来源:尧图网络
等待唤醒案例分析(线程之间的通信)
方法说明void wait()线程等待,等待的过程中线程会释放锁,需要被其他线程调用notify方法将其唤醒,重新抢锁执行但是并不会重新执行全部代码void notify()线程唤醒,一次唤醒一个等待线程;如果有多条线程等待,则随机唤醒一条等待线程void notifyAll()唤醒所有等待线程wait和notify方法需要锁对象调用,所以需要用到同步代码块中,而且必须是同一个锁对象2.等待唤醒案例实现/* count和flag可以定义成包装类 但是要记得给count和flag手动赋值 不然对于本案例来说,容易出现空指针异常 */ public class BaoZiPu { //代表包子的count private int count; //代表是否有包子的flag private boolean flag; public BaoZiPu() { } public BaoZiPu(int count, boolean flag) { this.count count; this.flag flag; } /* getCount 改造成消费包子方法 直接输出count */ public void getCount() { System.out.println(消费了..............第count个包子); } /* setCount 改造成生产包子 count */ public void setCount() { count; System.out.println(生产了...第count个包子); } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag flag; } }public class Product implements Runnable{ private BaoZiPu baoZiPu; public Product(BaoZiPu baoZiPu) { this.baoZiPu baoZiPu; } Override public void run() { while(true){ try { Thread.sleep(100L); } catch (InterruptedException e) { throw new RuntimeException(e); } synchronized (baoZiPu){ //1.判断flag是否为true,如果是true,证明有包子,生产线程等待 if (baoZiPu.isFlag()true){ try { baoZiPu.wait(); } catch (InterruptedException e) { throw new RuntimeException(e); } } //2.如果flag为false,证明没有包子,开始生产 baoZiPu.setCount(); //3.改变flag状态,为true,证明生产完了,有包子了 baoZiPu.setFlag(true); //4.唤醒消费线程 baoZiPu.notify(); } } } }public class Consumer implements Runnable{ private BaoZiPu baoZiPu; public Consumer(BaoZiPu baoZiPu) { this.baoZiPu baoZiPu; } Override public void run() { while(true){ try { Thread.sleep(100L); } catch (InterruptedException e) { throw new RuntimeException(e); } synchronized (baoZiPu){ //1.判断flag是否为false,如果是false,证明没有包子,消费线程等待 if (baoZiPu.isFlag()false){ try { baoZiPu.wait(); } catch (InterruptedException e) { throw new RuntimeException(e); } } //2.如果flag为true,证明有包子,开始消费 baoZiPu.getCount(); //3.改变flag状态,为false,证明消费完了,没 有包子了 baoZiPu.setFlag(false); //4.唤醒生产线程 baoZiPu.notify(); } } } }public class Test01 { public static void main(String[] args) { BaoZiPu baoZiPu new BaoZiPu(); Product product new Product(baoZiPu); Consumer consumer new Consumer(baoZiPu); Thread t1 new Thread(product); Thread t2 new Thread(consumer); t1.start(); t2.start(); } }总结在该实例中我们要考虑的是同步和互斥的问题我们要实现生产一个消费一个并且仅仅只有一个生产者和消费者这就要考虑到两个线程访问同一个资源的问题我们利用同步代码进行上锁处理还需要考虑生产和消费者的顺序问题我们使用wait和notify两个api进行实现。​ 我们利用同步代码在消费者和生产者中进行重写了run方法我们利用对象实例进行作为mutex互斥锁利用一个变量来判断是否已经生产还是消费若判断成功则放在该实例对象的等待队列中否则进行操作然后随机唤醒等待队列的进程。3.用同步方法改造等待唤醒案例/* count和flag可以定义成包装类 但是要记得给count和flag手动赋值 不然对于本案例来说,容易出现空指针异常 */ public class BaoZiPu { //代表包子的count private int count; //代表是否有包子的flag private boolean flag; public BaoZiPu() { } public BaoZiPu(int count, boolean flag) { this.count count; this.flag flag; } /* getCount 改造成消费包子方法 直接输出count */ public synchronized void getCount() { //1.判断flag是否为false,如果是false,证明没有包子,消费线程等待 if (this.flag false) { try { this.wait(); } catch (InterruptedException e) { throw new RuntimeException(e); } } //2.如果flag为true,证明有包子,开始消费 System.out.println(消费了..............第 count 个包子); //3.改变flag状态,为false,证明消费完了,没 有包子了 this.flag false; //4.唤醒生产线程 this.notify(); } /* setCount 改造成生产包子 count */ public synchronized void setCount() { //1.判断flag是否为true,如果是true,证明有包子,生产线程等待 if (this.flag true) { try { this.wait(); } catch (InterruptedException e) { throw new RuntimeException(e); } } //2.如果flag为false,证明没有包子,开始生产 count; System.out.println(生产了...第 count 个包子); //3.改变flag状态,为true,证明生产完了,有包子了 this.flag true; //4.唤醒消费线程 this.notify(); } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag flag; } }public class Product implements Runnable{ private BaoZiPu baoZiPu; public Product(BaoZiPu baoZiPu) { this.baoZiPu baoZiPu; } Override public void run() { while(true){ try { Thread.sleep(100L); } catch (InterruptedException e) { throw new RuntimeException(e); } baoZiPu.setCount(); } } }public class Consumer implements Runnable{ private BaoZiPu baoZiPu; public Consumer(BaoZiPu baoZiPu) { this.baoZiPu baoZiPu; } Override public void run() { while(true){ try { Thread.sleep(100L); } catch (InterruptedException e) { throw new RuntimeException(e); } baoZiPu.getCount(); } } }public class Test01 { public static void main(String[] args) { BaoZiPu baoZiPu new BaoZiPu(); Product product new Product(baoZiPu); Consumer consumer new Consumer(baoZiPu); Thread t1 new Thread(product); Thread t2 new Thread(consumer); t1.start(); t2.start(); } }
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/10/30 4:02:09

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/10/29 23:30:48

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/10/29 13:36:24

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/10/29 19:55:35

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/10/29 13:36:50

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/10/28 16:06:10

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉