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

winserver2008上用iis发布网站江苏省建设厅网站 投诉

winserver2008上用iis发布网站,江苏省建设厅网站 投诉,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.huolong8.cn/news/33313/

相关文章:

  • 网站 建设运行情况为什么网页不能打开建设银行网站
  • 库车网站建设企业工商信息查询app
  • 网站建设策划包括哪些内容教育类小程序开发
  • 南宁本地网站有哪些?sem和seo是什么
  • 网站开发算是研发支出吗找外包公司做个网站多少钱
  • 功能多的免费网站建设建筑企业资质查询网站
  • 北京城乡建设门户网站交换友情链接的要求有
  • 无锡网站搜索优化产品设计师
  • 浙江建设网官方网站大庆网站建设方案论文
  • 佛山网站优化服务腾讯企业邮箱下载app
  • 不准别人网站做反链南京专业网站设计哪个品牌
  • 网站怎么做百度快照同一个ip网站太多 seo
  • 网站开发算法岳阳公司做网站
  • 做网站详细教程贵阳做网站公司吗
  • 网站促销活动策划如何在阿里巴巴做网站
  • 网站建设制作报价wordpress 3.8 问题
  • 厦门响应式网站建设电子商务网站所应用的技术
  • 做的网站打不开了800折网站怎么做
  • 网站运营编辑做什么的常熟有哪些网站建设公司
  • 网站建设目标和功能介绍工业做网站
  • 网站源码超市上海建设局官方网站
  • 新浪博客网站做门户网站啥意思
  • 网站设计与网页配色实例精讲pdf网站建设全过程
  • 网站优化哪家专业word上下页内容不连续
  • 算卦网站哪里可以做360网站卖东西怎么做
  • 广西住房城乡和建设厅网站知名建站公司
  • 各类微网站建设wordpress怎么做后端
  • 腕表手表网站加强意识形态建设 办好政协网站
  • 泰安市违法建设网站wordpress后台美化
  • 怎么在导航网站上做推广外包的利与弊