news 2026/6/24 17:24:52

Java线程间通信

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Java线程间通信

       我们这里使用两个线程分别充当生产者和消费者,对资源res进行共享,并通过res进行通信,其中用到了同步锁、wait、notify、sleep等方法。

解法1:见下面代码。

//资源,我们这里表示煤,分精煤和烟煤 class Resource { String name; int weight; // 表示煤是否已经拉来,true表示煤已拉来 boolean flag = false; // 拉煤的总车数 int numCars = 0; } // Input代表拉煤的小卡车 class Input implements Runnable { Resource res; public Input(Resource res) { this.res = res; } @Override public void run() { int x = 0; while (true) { synchronized (res) { if (res.numCars > 99) { break; } if (!res.flag) { try { Thread.sleep(10); } catch (Exception e) { } if (x == 0) { res.name = "精煤"; // 表示一次拉6吨精煤 res.weight = 6; } else { res.name = "烟煤"; // 表示一次拉10吨烟煤 res.weight = 10; } x = (x + 1) % 2; res.flag = true; res.numCars++; System.out.println(Thread.currentThread().getName() + "拉来一车重" + res.weight + "吨的" + res.name + ";这是第" + res.numCars + "车煤。"); // 拉来煤之后就等待消耗,直到锅炉消耗完,通知小卡车去拉煤 try { res.wait();// 会释放锁,而sleep方法不释放锁 } catch (InterruptedException e) { e.printStackTrace(); } } } } } } // Output代表锅炉,用来消耗煤 class Output implements Runnable { Resource res; public Output(Resource res) { this.res = res; } @Override public void run() { while (true) { synchronized (res) { if (res.flag) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "烧了一车重" + res.weight + "吨的" + res.name + ";这是第" + res.numCars + "车煤。"); res.name = null; res.weight = 0; res.flag = false; // 煤消耗完了,通知小卡车拉煤去 res.notify(); } if (res.numCars > 99) { break; } } } } } public class ThreadCommunication { public static void main(String[] args) { Resource resource = new Resource(); Input input = new Input(resource); Output output = new Output(resource); //线程bob充当小卡车 Thread bob = new Thread(input, "Bob"); //线程mike充当锅炉 Thread mike = new Thread(output, "Mike"); bob.start(); mike.start(); } }

解法2:上面的代码进行优化,进行了顺序调整,增加了合理化的判断。

//资源,我们这里表示煤,分精煤和烟煤 class Resource { String name; int weight; // 表示煤是否已经拉来,true表示煤已拉来 boolean flag = false; // 拉煤的总车数 int numCars = 0; } // Input代表拉煤的小卡车 class Input implements Runnable { Resource res; public Input(Resource res) { this.res = res; } @Override public void run() { int x = 0; while (true) { synchronized (res) { if (res.numCars > 99) { break; } if (res.flag) {// 有煤就睡觉休息,等待唤醒 try { res.wait();// 会释放锁,而sleep方法不释放锁 } catch (InterruptedException e) { e.printStackTrace(); } } try { Thre
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/23 19:08:04

Jasminum插件:中文文献元数据智能提取的完整解决方案

Jasminum插件:中文文献元数据智能提取的完整解决方案 【免费下载链接】jasminum A Zotero add-on to retrive CNKI meta data. 一个简单的Zotero 插件,用于识别中文元数据 项目地址: https://gitcode.com/gh_mirrors/ja/jasminum 在中文学术研究领…

作者头像 李华
网站建设 2026/6/23 11:21:35

如何用Jasminum插件彻底解决中文文献管理难题?

还在为中文文献元数据抓取困难而烦恼吗?每次添加知网PDF都要手动输入作者、标题、期刊信息?Jasminum插件作为专为Zotero设计的终极解决方案,能够快速抓取知网元数据,让你告别繁琐的手动录入! 【免费下载链接】jasminum…

作者头像 李华
网站建设 2026/6/23 15:58:49

Zotero插件市场:让文献管理更智能的5个关键体验

Zotero插件市场:让文献管理更智能的5个关键体验 【免费下载链接】zotero-addons Zotero add-on to list and install add-ons in Zotero 项目地址: https://gitcode.com/gh_mirrors/zo/zotero-addons 还在为Zotero插件安装的繁琐步骤而烦恼吗?现在…

作者头像 李华
网站建设 2026/6/24 8:43:06

基于计算机视觉的视频PPT自动提取技术方案

基于计算机视觉的视频PPT自动提取技术方案 【免费下载链接】extract-video-ppt extract the ppt in the video 项目地址: https://gitcode.com/gh_mirrors/ex/extract-video-ppt 问题背景:视频课件整理的效率瓶颈 在当今数字化教育和工作环境中,…

作者头像 李华
网站建设 2026/6/23 14:46:27

上门按摩现在还能做吗

看着门店的客流时好时坏,听着同行嘴里上门模式又赚了多少订单,你是否也在犹豫——自己到底要不要也做一个上门按摩的平台?你是否也在纠结——这个上门模式真的没有风险吗?作为手握技师资源的我们,既害怕错过线上的流量…

作者头像 李华
网站建设 2026/6/23 20:34:46

PMO实战:AI研发效能度量(DORA×SPACE)路线图

DORA 2025 报告指出:AI 采用率上升可能伴随吞吐与稳定波动,根因在于交付基本功与治理护栏没跟上。本文用 DORASPACE 给 PMO 一套 AI 研发效能度量路线图:先对齐口径,再做可对照试点,最后规模化治理,并说明如…

作者头像 李华