java线 生产者和消费者
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
package org.rui.thread.block; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; /**
* 生产者与消费者
* 餐馆
*
* @author lenovo
*
*/
public class Restaurant { //Restaurant r=new Restaurant(); Meal meal;
ExecutorService exec = Executors.newCachedThreadPool();
WaitPerson waitP = new WaitPerson(this);
Chef chef = new Chef(this); public Restaurant() {
exec.execute(chef);
exec.execute(waitP);
} public static void main(String[] args) {
new Restaurant();
} } // 餐
class Meal {
private final int orderNum; public Meal(int orderNum) {
this.orderNum = orderNum;
} public String toString() {
return "Meal " + orderNum;
} } // 服务员
class WaitPerson implements Runnable {
private Restaurant restaurant; public WaitPerson(Restaurant r) {
restaurant = r;
} public void run() { try {
while (!Thread.interrupted()) { synchronized (this) {
while (restaurant.meal == null)//假设餐为空,服务员等待
wait();
}
System.out.println("waitperson got " + restaurant.meal);
//在厨师上同步 上完餐 并通知厨师
synchronized (restaurant.chef) {
restaurant.meal = null;
restaurant.chef.notifyAll();// 准备还有一个
}
}
} catch (InterruptedException e) {
System.out.println("WaitPerson interrupted");
}
}
} //厨师
class Chef implements Runnable { Restaurant restaurant;
private int count = 0; public Chef(Restaurant r) {
restaurant = r;
} @Override
public void run() {
try {
while (!Thread.interrupted()) {
/**
* 厨师送上餐,并通知服务员。就等待 。直至服务员收集到订单并通知厨师
*/
synchronized (this) {
while (restaurant.meal != null)//就等待 while 防止错失信号的程序设计(假设if 可能会有其它线程插足拿走订单)
wait();
}
if (++count == 10) {
System.out.println("out of food, closing");
restaurant.exec.shutdownNow();
}
System.out.print("order up!");
//在服务员锁上同步
synchronized (restaurant.waitP) {
restaurant.meal = new Meal(count);//送上餐
restaurant.waitP.notifyAll();//通知服务员
}
TimeUnit.MILLISECONDS.sleep(100);
} } catch (InterruptedException e) {
System.out.println("Chef interrupted");
} } }
/**
output:
order up!waitperson got Meal 1
order up!waitperson got Meal 2
order up!waitperson got Meal 3
order up!waitperson got Meal 4
order up!waitperson got Meal 5
order up!waitperson got Meal 6
order up!waitperson got Meal 7
order up!waitperson got Meal 8
order up!waitperson got Meal 9
out of food, closing
WaitPerson interrupted
order up!Chef interrupted */
java线 生产者和消费者的更多相关文章
- Windows下RabbitMQ 的下载、配置、Java实现生产者和消费者例子
RabbitMQ是一个轻量级的消息代理中间件,支持多种消息通信协议,支持分布式部署,支持运行于多个操作系统,具有灵活.高可用等特性.RabbitMQ支持多种协议,其中最为重要的是高级消息队列协议(AM ...
- java实现生产者和消费者问题
Java实现生产者和消费者问题 欢迎访问我的个人博客,获取更多有用的东西 链接一 链接二 也可以关注我的微信订阅号:CN丶Moti
- Java实现生产者和消费者
生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...
- java之生产者与消费者
package com.produce; import java.util.LinkedList; import java.util.Queue; /*@author shijin * 生产者与消费者 ...
- Java实现生产者与消费者模式
生产者不断向队列中添加数据,消费者不断从队列中获取数据.如果队列满了,则生产者不能添加数据:如果队列为空,则消费者不能获取数据.借助实现了BlockingQueue接口的LinkedBlockingQ ...
- Java中生产者与消费者模式
生产者消费者模式 首先来了解什么是生产者消费者模式.该模式也称有限缓冲问题(英语:Bounded-buffer problem),是一个多线程同步问题的经典案例.该问题描述了两个共享固定大小缓冲区的线 ...
- java之生产者和消费者问题
package testThread; public class Test3 { public static void main(String[] args) { Clerk c = new Cler ...
- 菜鸡的Java笔记 生产者与消费者
生产者与消费者 代码要求知道做什么用即可 线程间的通讯问题以及 Object 类的支持 基础模型 现在希望实现一种数据的生产和取出的操作 ...
- Java 多线程-生产者、消费者
一.整体代码 ThreadDemo.java public class ThreadDemo { public static void main(String[] args) { Godown god ...
随机推荐
- centos安装和卸载软件
==如何卸载: 1.打开一个SHELL终端 2.因为Linux下的软件名都包括版本号,所以卸载前最好先确定这个软件的完整名称. 查找RPM包软件:rpm -qa ×××* 注意:×××指软件名称开头的 ...
- 【游戏】2048及各种变种大集合汇总【更新ing~新版Floppy2048 - 恒星聚变版 - 恶搞改数据】
threes - 鼻祖 手机版:http://asherv.com/threes/ js版:http://threesjs.com/ 2048 - 原版 http://gabrielecirulli. ...
- Windows在结构Eclipse+Android4.0开发环境
官方搭建步骤: http://developer.android.com/index.html 搭建好开发环境之前须要下载以下几个文件包: 一.安装Java执行环境JRE(没这个Eclipse执行不起 ...
- appendChild的用法
appendChild的用法 1,先把元素从原有父级上删除 2,再把元素添加到新父级上
- HDU 1698 Just a Hook (段树更新间隔)
Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing for m ...
- HDU 1274 展开字符串 (递归+string类)
题目链接:HDU 1274 展开字符串 中文题. 左括号进入DFS函数,右括号return到上一层. 注意return回去的是这个一层递归中的括号中的字母串. AC代码: #include<st ...
- 安装framework 3.5出现0x800F0922的解决方法
关闭Windows 防火墙,启动Windows model install服务 重启! 再安装 就ok了
- HDU 1085-Holding Bin-Laden Captive!(生成功能)
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 4916 树形dp
Count on the path Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- Python学习笔记23:Django构建一个简单的博客网站(一个)
在说如何下载和安装Django,本节将重点讨论如何使用Django站点. 一 新建project 命令:django-admin startproject mysite # 有的须要输入:django ...