news 2026/4/15 11:00:48

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/4/13 9:09:43

Hitboxer:键盘操作优化的实用工具解析

Hitboxer:键盘操作优化的实用工具解析 【免费下载链接】socd SOCD cleaner tool for epic gamers 项目地址: https://gitcode.com/gh_mirrors/so/socd 在竞技游戏领域,键盘操作的精确定位往往决定了胜负的关键。当玩家同时按下相反方向键时&#…

作者头像 李华
网站建设 2026/4/13 11:16:49

MAA自动公招终极指南:5分钟学会智能招募技巧

MAA自动公招终极指南:5分钟学会智能招募技巧 【免费下载链接】MaaAssistantArknights 一款明日方舟游戏小助手 项目地址: https://gitcode.com/GitHub_Trending/ma/MaaAssistantArknights MAA自动公招系统是《明日方舟》玩家的得力助手,通过智能化…

作者头像 李华
网站建设 2026/4/10 6:37:04

罗技鼠标宏配置终极指南:从零到精通的实战教程

还在为游戏中的射击稳定性而困扰?想要在激烈的竞技中保持完美的压枪效果?这篇罗技鼠标宏配置指南将带你从基础概念到高级技巧,一步步掌握游戏鼠标宏的强大功能。 【免费下载链接】logitech-pubg PUBG no recoil script for Logitech gaming m…

作者头像 李华
网站建设 2026/4/15 9:18:18

TranslucentTB 终极使用教程:打造完美透明任务栏的完整指南

TranslucentTB 终极使用教程:打造完美透明任务栏的完整指南 【免费下载链接】TranslucentTB A lightweight utility that makes the Windows taskbar translucent/transparent. 项目地址: https://gitcode.com/gh_mirrors/tr/TranslucentTB 想要让Windows桌面…

作者头像 李华
网站建设 2026/4/4 3:17:13

如何在VMware中完美运行macOS:解锁工具Unlocker 3.0终极指南

想要在VMware虚拟机中体验苹果macOS系统,却被兼容性限制所困扰?Unlocker 3.0正是解决这一难题的终极开源工具,它能一键解除VMware对macOS的限制,让普通PC也能流畅运行苹果操作系统。作为VMware macOS解锁工具的标杆产品&#xff0…

作者头像 李华
网站建设 2026/4/13 15:14:59

ArduPilot航拍安全返航机制:全面讲解

ArduPilot航拍安全返航机制:从原理到实战的深度解析当失控来临,无人机如何“自己回家”?你有没有过这样的经历:正在城市高楼间操控无人机拍摄延时视频,突然遥控器信号断了;或者在偏远山区执行测绘任务时&am…

作者头像 李华