问题描述

假设分别有4位厨师和6位食客。

厨师做一盘菜的时间是4S,食客吃一盘菜的时间是3S。

每位厨师做好菜后放入有固定容量(10盘)的桌子上。

如果厨师做好菜发现桌子上已经有10盘菜了,就必须等待任意一个食客吃掉一盘后才能放入;

如果食客取菜时发现桌子上没有菜,也必须等待有任一厨师做好菜放入桌子才能取用。

代码:

Test类:

public class Test{

public static void main(String[] args) throws InterruptedException {

// TODO Auto-generated method stub

Table t=new Table(10);

new Cook(t).start();

new Cook(t).start();

new Cook(t).start();

new Cook(t).start();

new Diners(t).start();

new Diners(t).start();

new Diners(t).start();

new Diners(t).start();

new Diners(t).start();

new Diners(t).start();

}

}

Table类:

import java.util.LinkedList;

@SuppressWarnings("serial")

class Table extends LinkedList {

int maxSize; // 容器的最大容量

public Table(int maxSize) {

this.maxSize = maxSize;

}

public synchronized void putFood(Food f) { // 向容器内放置食品

while (this.size() >= this.maxSize) {

try {

System.out.println("The table is too full,wait a moment!");

wait();

} catch (Exception e) {

}

}

this.addLast(f);

System.out.println("上了一份"+f+",现在桌上有"+this.size()+"份菜**");

notifyAll();

}

public synchronized Food getFood() { // 从容器内取食品

Food f;

while (this.size() <= 0) {

try {

System.out.println("There is no food now ,come here later!");

wait();

} catch (Exception e) {

}

}

f = (Food) this.getFirst();

this.remove(f);

System.out.println("吃了一份"+f+",现在桌上有"+this.size()+"份菜");

notifyAll();

return f;

}

}

Cook类

public class Cook extends Thread {

private Table t;

Cook( Table t) {

this.t = t;

}

public void run() {

while (true) {

Food f = name();

try{

Thread.sleep(4000);

}catch(InterruptedException e){}

t.putFood(f);

}

}

private Food name() {

// TODO Auto-generated method stub

return new Food();

}

}

Diners类:

public class Diners extends Thread {

private Table t = new Table(getPriority());

Diners(Table t){

this.t=t;

}

public void run(){

while(true){

Food f=t.getFood();

try{

Thread.sleep(3000);

}catch(InterruptedException e){}

}

}

}

Food类:

class Food {

} // 食品类

运行结果:

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

上了一份jsp_ex2.Food@783e9d8b,现在桌上有1份菜**

吃了一份jsp_ex2.Food@783e9d8b,现在桌上有0份菜

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

上了一份jsp_ex2.Food@30681e13,现在桌上有1份菜**

上了一份jsp_ex2.Food@b468cdf,现在桌上有2份菜**

上了一份jsp_ex2.Food@302702c3,现在桌上有3份菜**

吃了一份jsp_ex2.Food@30681e13,现在桌上有2份菜

吃了一份jsp_ex2.Food@b468cdf,现在桌上有1份菜

吃了一份jsp_ex2.Food@302702c3,现在桌上有0份菜

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

上了一份jsp_ex2.Food@76f96cfc,现在桌上有1份菜**

吃了一份jsp_ex2.Food@76f96cfc,现在桌上有0份菜

There is no food now ,come here later!

There is no food now ,come here later!

There is no food now ,come here later!

上了一份jsp_ex2.Food@58163c7,现在桌上有1份菜**

上了一份jsp_ex2.Food@3eaff66e,现在桌上有2份菜**

吃了一份jsp_ex2.Food@58163c7,现在桌上有1份菜

吃了一份jsp_ex2.Food@3eaff66e,现在桌上有0份菜

There is no food now ,come here later!

There is no food now ,come here later!

上了一份jsp_ex2.Food@3ee0fab7,现在桌上有1份菜**

吃了一份jsp_ex2.Food@3ee0fab7,现在桌上有0份菜

There is no food now ,come here later!

There is no food now ,come here later!

程序没写菜名,简单实现....

java多线程厨师做饼,Java多线程之厨师与食客问题相关推荐

  1. 生产者(厨师做面条)消费者(吃货吃面条)模式synchronized 法和Lock+Condition法(不使用集合),代码详解

    1. synchronized + wait + notifyAll synchronized:包裹着同步代码块,代码块执行完释放锁 wait:线程挂起,失去锁 notifyAll:唤醒同个对象的wa ...

  2. Java总结篇系列:Java多线程(三)

    2019独角兽企业重金招聘Python工程师标准>>> 本文主要接着前面多线程的两篇文章总结Java多线程中的线程安全问题. 一.一个典型的Java线程安全例子 public cla ...

  3. 深入理解多线程(五)—— Java虚拟机的锁优化技术

    本文是<深入理解多线程>的第五篇文章,前面几篇文章中我们从synchronized的实现原理开始,一直介绍到了Monitor的实现原理. 前情提要 通过前面几篇文章,我们已经知道: 1.同 ...

  4. java如何写线程外部类_廖雪峰Java读书笔记(六)--多线程(或称并发)

    1. 多线程基础 首先要明白一些概念: 进程:把一个任务称为一个进程,浏览器就是一个进程,视频播放器是另一个进程,类似的,音乐播放器和Word都是进程. 线程:某些进程内部还需要同时执行多个子任务.例 ...

  5. java 并发编程多线程_多线程(一)java并发编程基础知识

    线程的应用 如何应用多线程 在 Java 中,有多种方式来实现多线程.继承 Thread 类.实现 Runnable 接口.使用 ExecutorService.Callable.Future 实现带 ...

  6. java多线程编程_《java多线程编程实战指南》读书笔记 -- 基本概念

    展开 并发:多个线程操作相同资源,保证线程安全,合理使用资源 高并发:服务能同时处理多个请求,提高程序性能 测试上下文切换工具 Lmbench3 测量上下文切换时长 vmstat 测量上下文切换次数 ...

  7. java优先队列_Java高级特性增强-多线程

    请戳GitHub原文: https://github.com/wangzhiwubigdata/God-Of-BigData 大数据成神之路系列: 请戳GitHub原文: https://github ...

  8. JAVA基础学习day25--Socket基础二-多线程

    一.上传图片 1.1.示例 /* 上传图片 */ import java.net.*; import java.io.*; import java.util.*; import java.text.* ...

  9. JAVA高并发学习笔记(二) 多线程基础

    1.1什么是线程 线程是进程(程序在计算机上的一次执行活动)内的执行单元 进程是以独立于其他进程的方式运行的,进程间是互相隔离的.一个进程无法直接访问另一个进程的数据.进程的资源诸如内存和CPU时间片 ...

最新文章

  1. python 正则表达式 前瞻_正则表达式 For Python
  2. [linux]Linux挂载光盘,解压光盘内的tar安装包,并安装软件(Install VMware Tools in a Linux Guest)...
  3. JVM 优点与缺点的深入分析
  4. python图片比对、自动化测试,腾讯优图及知脸(ZKface)人脸比对接口测试(python)
  5. 面向对象实现放大镜_面向音乐家和音乐爱好者的开放式硬件:耳机,放大器等
  6. android sudio jni 调用so_Android NDK-深入理解JNI
  7. 【评分】个人作业——软件工程实践总结作业
  8. 基于visual Studio2013解决面试题之1404希尔排序
  9. Linux brctl 命令,虚拟网络设备 LinuxBridge 管理工具
  10. php怎么创建以太坊钱包地址,php如何调用以太坊JSON-RPC接口创建钱包
  11. 研究了那么多内容后,我们打算推荐这些公众号给你
  12. Windows 搭建Syslog、RSyslog日志服务器
  13. 【今日小记】程序员的孤独,没人懂
  14. PHP的implode函数运用,PHP implode()函数用法讲解
  15. Kubernetes HPA 动态弹性扩缩容
  16. java实习生简历模板
  17. 编程方式操作WorkFlow
  18. 百丽三十而立:“鞋王”DTC之路的阵痛与重生
  19. busybox 的insmod can't insert operation not permitted 错误解决
  20. JAVA素因子只有3 5 7_第k个数

热门文章

  1. 浅谈Observer观察者模式
  2. word wps 出版 常用操作
  3. 浏览器如何截图整个滚动屏 ?
  4. LJX的校园:社会实践的任务
  5. Linux开放MySql 3306端口
  6. hook koa web 码云_Doodoo.js 发布 1.1.0,Koa.js+ Nuxt.js 最佳实践
  7. 电脑Mac地址更改后有什么害处?怎么改回原来的?
  8. Android 多种限定符
  9. 2021年煤矿瓦斯检查证考试及煤矿瓦斯检查模拟考试题
  10. HTTP 的前世今生