news 2026/7/11 14:34:23

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/7/11 7:50:03

Zotero插件生态终极指南:一键打造专属文献管理神器

Zotero插件生态终极指南:一键打造专属文献管理神器 【免费下载链接】zotero-addons Zotero add-on to list and install add-ons in Zotero 项目地址: https://gitcode.com/gh_mirrors/zo/zotero-addons 还在为Zotero插件安装的繁琐流程而烦恼吗?…

作者头像 李华
网站建设 2026/7/11 8:25:36

haxm is not installed 错误解决:完整指南与排查步骤

彻底解决 “haxm is not installed” 错误:从原理到实战的完整排查手册 你有没有遇到过这样的场景? 刚配置好 Android Studio,满怀期待地点击运行按钮启动模拟器,结果弹出一条令人沮丧的提示: HAXM is not installe…

作者头像 李华
网站建设 2026/7/10 4:33:48

Soundflower终极指南:零基础玩转macOS音频路由神器

Soundflower终极指南:零基础玩转macOS音频路由神器 【免费下载链接】Soundflower MacOS system extension that allows applications to pass audio to other applications. 项目地址: https://gitcode.com/gh_mirrors/sou/Soundflower 还在为macOS无法跨应用…

作者头像 李华
网站建设 2026/7/11 0:17:04

MAA自动公招终极指南:从零基础到快速上手的完整教程

MAA自动公招终极指南:从零基础到快速上手的完整教程 【免费下载链接】MaaAssistantArknights 一款明日方舟游戏小助手 项目地址: https://gitcode.com/GitHub_Trending/ma/MaaAssistantArknights MAA自动公招系统是《明日方舟》玩家的智能助手,通…

作者头像 李华
网站建设 2026/7/10 20:05:25

BabelDOC:零基础PDF文档翻译与双语生成完整指南

BabelDOC:零基础PDF文档翻译与双语生成完整指南 【免费下载链接】BabelDOC Yet Another Document Translator 项目地址: https://gitcode.com/GitHub_Trending/ba/BabelDOC 在全球信息交流日益频繁的今天,PDF文档的跨语言翻译需求急剧增长。Babel…

作者头像 李华
网站建设 2026/7/10 14:14:52

罗技PUBG压枪宏配置全攻略:告别枪口抖动困扰

罗技PUBG压枪宏配置全攻略:告别枪口抖动困扰 【免费下载链接】logitech-pubg PUBG no recoil script for Logitech gaming mouse / 绝地求生 罗技 鼠标宏 项目地址: https://gitcode.com/gh_mirrors/lo/logitech-pubg 还在为PUBG中枪口乱跳而烦恼吗&#xff…

作者头像 李华