当前位置: 首页 > news >正文

阳泉市网站建设_网站建设公司_模板建站_seo优化

电子商城网站开发项目经验,用wordpress做网站,成都网站搜索引擎优化,临海门户网站住房和城乡建设规划局编写一个Java程序#xff0c;其中包含三个线程: 厨师(Chef)、服务员(Waiter)和顾客(Customer)。他们的行动如下: 厨师准备菜肴#xff0c;每次准备一个。服务员等待菜肴准备好#xff0c;然后将其送到顾客那里。顾客等待服务员送来菜看后才开始吃。所有三个角色应该循环进行…编写一个Java程序其中包含三个线程: 厨师(Chef)、服务员(Waiter)和顾客(Customer)。他们的行动如下: 厨师准备菜肴每次准备一个。服务员等待菜肴准备好然后将其送到顾客那里。顾客等待服务员送来菜看后才开始吃。所有三个角色应该循环进行他们的行为直到指定数量的菜肴被制作和消费完。使用wait()和notify0)确保线程间正确的通信和同步。 package com;/*** program: RestaurantDemo* description: 餐厅类* author: Casey Hu* create: 2023-12-18 16:13**/public class Restaurant {public static void main(String[] args) throws InterruptedException {Chef chef new Chef();Waiter waiter new Waiter(chef);Customer customer new Customer(waiter);chef.start();waiter.start();customer.start();chef.join();waiter.join();customer.join();System.out.println(餐厅关闭.);}static class Chef extends Thread {private final int MAX_FOOD 10;private int foodCount 0;private boolean isFinished false;Overridepublic void run() {while (!isFinished) {try {// 厨师准备菜肴System.out.println(厨师准备菜肴...);Thread.sleep(1000);// 菜肴准备好后唤醒服务员foodCount;synchronized (this) {this.notify();}if (foodCount MAX_FOOD) {isFinished true;}} catch (InterruptedException e) {e.printStackTrace();}}}}static class Waiter extends Thread {private Chef chef;public Waiter(Chef chef) {this.chef chef;}Overridepublic void run() {while (!chef.isFinished) {try {synchronized (chef) {// 等待厨师准备菜肴chef.wait();}// 将菜肴送到顾客那里System.out.println(服务员将菜肴送到顾客那里...);Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}static class Customer extends Thread {private Waiter waiter;public Customer(Waiter waiter) {this.waiter waiter;}Overridepublic void run() {while (!waiter.chef.isFinished) {try {// 等待服务员送来菜肴Thread.sleep(1000);// 开始吃菜肴System.out.println(顾客开始吃菜肴...);// 通知服务员可以送下一道菜了synchronized (waiter.chef) {waiter.chef.notify();}} catch (InterruptedException e) {e.printStackTrace();}}}} } 上述代码中启动了三个子线程厨师线程、服务员线程和顾客线程。厨师线程使用foodCount计数器来记录制作的菜肴数量并使用synchronized关键字确保线程安全。每当它制作好一道菜肴后它唤醒等待的服务员线程。 服务员线程使用synchronized和wait()方法等待厨师线程准备好菜肴。一旦收到通知它将菜肴送到顾客那里。 顾客线程使用synchronized关键字和notify()方法通知服务员准备下一道菜肴并在接收到菜肴后开始吃。 主线程使用join()方法等待所有子线程完成。 这个示例使用了Java中的核心多线程机制(Thread、Runnable、synchronized、wait()和notify()等)来模拟餐厅的运作。 运行结果 但是有一段代码 public class Restaurant {private static final int MAX_FOOD 10;private static final Object lock new Object();private static int foodCount 0;public static void main(String[] args) {Chef chef new Chef();Waiter waiter new Waiter();Customer customer new Customer();chef.start();waiter.start();customer.start();}static class Chef extends Thread {Overridepublic void run() {synchronized (lock) {while (foodCount MAX_FOOD) {try {// 厨师准备菜肴System.out.println(厨师准备菜肴...);Thread.sleep(1000);// 菜肴准备好后唤醒服务员foodCount;lock.notify();} catch (InterruptedException e) {e.printStackTrace();}}}}}static class Waiter extends Thread {Overridepublic void run() {synchronized (lock) {while (foodCount MAX_FOOD) {try {// 等待菜肴准备好lock.wait();// 将菜肴送到顾客那里System.out.println(服务员将菜肴送到顾客那里...);Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}}static class Customer extends Thread {Overridepublic void run() {synchronized (lock) {while (foodCount MAX_FOOD) {try {// 等待菜肴送来lock.wait();// 开始吃菜肴System.out.println(顾客开始吃菜肴...);Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}} } 运行结果 就是厨师准备好菜肴之后main线程结束 所以才会选择子线程的方式有大佬知道为啥会出现这样的结果吗评论区聊聊
http://www.ihoyoo.com/news/53619.html

相关文章:

  • 专业版装修用什么网站做导航条服装定制行业的未来和趋势
  • 高校保卫处网站建设工作总结中文的网站做不成二维码
  • 网站是否有管理员权限盘锦网站建设
  • 新网站怎么发外链昆明智能建站
  • 生活门户网站开发方案编写网页所用的语言是
  • 专门做任务的网站设计网页制作与
  • 怎么分析竞争对手网站云盘网站建设
  • 苏州家教网站建设做公司网站客户群体怎么找
  • 怎么把服务器做网站百度竞价网站备案
  • 重庆一次可以备案多少个网站网站开发的经济可行性分析
  • 有些中小网站汕头做网站的公司
  • 网站被备案能建设wordpress可以建官网嘛
  • 怎么用图片做网站背景图地方网站商城怎么做
  • 中山专业外贸网站建设wordpress中文 插件
  • 做地方特产的网站百度网页版主页
  • 高端企业网站报价wordpress插件怎么使用教程
  • 做蛋糕的英文网站logo设计在线生成免费免费设计logo生成器
  • 如何编写网站泉州市建设局网站公示
  • 贵州网推传媒有限公司网站seo快速排名优化的软件
  • 这里是我做的网站商标号在线查询
  • 做农村网站多少钱网易企业邮箱小程序
  • 谷歌官方网站首页网站维护经费
  • wordpress博客 分类长沙网站优化诊断
  • 建设一元云购类网站青岛网站美工
  • 廊坊专业网站制作服务上海市建设工程招投标信息网
  • 怎么建个免费英文网站网站策划与运营课程认知
  • 网站搭建的注意事项泰安招聘网站有哪些
  • 网站正在建设中 手机版关于协会网站建设的意见
  • 西安汽车网站制作我要创业找项目
  • 南京做网站价格小程序图片制作