`
zsnlovewl
  • 浏览: 172975 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

生产者与消费者例子(多线程wait()与notifyAll应用)

    博客分类:
  • JAVA
阅读更多
package test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

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;
	}
	@Override
	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{
	private 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){
						wait();//
					}
				}
				if(++count==10){
					System.out.println("out of food, closing");
					restaurant.exec.shutdownNow();
				}
				System.out.print("Order up!");
				synchronized(restaurant.waitPerson){
					restaurant.meal=new Meal(count);
					restaurant.waitPerson.notifyAll();
				}
				TimeUnit.MILLISECONDS.sleep(100);
			}
		} catch (InterruptedException e) {
			System.out.println("Chef interrupted");
		}
		
	}
	
}
public class Restaurant {

	/**
	 * @param args
	 */
	Meal meal;
	ExecutorService exec=Executors.newCachedThreadPool();
	WaitPerson waitPerson=new WaitPerson(this);
	Chef chef=new Chef(this);
	public Restaurant(){
		exec.execute(chef);//启动chef线程
		exec.execute(waitPerson);//启动waitPerson线程
	}
	public static void main(String[] args) {
		new Restaurant();

	}

}

 

分享到:
评论

相关推荐

    如何在Java中正确使用 wait, notify 和 notifyAll

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视。本文对这些关键字的使用进行了描述。  在 Java 中可以用 wait、notify 和 notifyAll 来实现...

    【并发编程】 — 线程间的通信wait、notify、notifyAll

    三个线程顺序打印问题2.1.1 题目2.1.2 题目分析2.1.3 我的答案2.2 生产者消费者问题2.2.1 题目2.2.2 题目分析2.2.3 我的答案 源码地址:https://github.com/nieandsun/concurrent-study.git 1 wait、notify、...

    Java多线程的等待唤醒机制代码演示通俗易懂

    生产者和消费者是一个十分经典的多线程协作模式 **常见方法:** - void wait() 当前线程等待,直到被其他线程唤醒 - void notify() 随机唤醒单个线程 - void notifyAll() 唤醒所有线程

    Day 22多线程、线程通信、线程池和Lambda表达式

    Day 22 Author:ScorpioDong 1. 多线程 1.1 WAITING(无限等待) 当某一线程被执行wait()方法,需要等待其他线程...现在存在两个完全无关的线程,生产者和消费者,但是商品会作为他们两者之间的共享资源。 生产者和消费

    java多线程通信之等待唤醒机制

    典型实例有生产者和消费者,本文也通过实例来分析线程等待唤醒机制。  1、相关API介绍  public final void notify()  唤醒在此对象监视器上等待的单个线程,如果所有线程都在此对象上等待,则会任意选择唤醒...

    第22天

    如果是生产者与消费者的情况时,若wait()方法不在同步代码块中,则会产生丢失唤醒的骚操作,即生产者生产完成时,还未做出wait等待时,被消费者插队取走了货物。而此时消费者发出notify信号,但是生产者不会接收...

    汪文君高并发编程实战视频资源全集

    │ 高并发编程第一阶段26讲、多线程下的生产者消费者模型,以及详细介绍notifyAll方法.mp4 │ 高并发编程第一阶段27讲、wait和sleep的本质区别是什么,深入分析(面试常见问题).mp4 │ 高并发编程第一阶段28讲、...

    汪文君高并发编程实战视频资源下载.txt

    │ 高并发编程第一阶段26讲、多线程下的生产者消费者模型,以及详细介绍notifyAll方法.mp4 │ 高并发编程第一阶段27讲、wait和sleep的本质区别是什么,深入分析(面试常见问题).mp4 │ 高并发编程第一阶段28讲、...

    javaSE代码实例

    16.4.5 “生产者-消费者”案例的实际运行 365 16.4.6 notify方法的使用 366 16.4.7 同步的语句块 367 16.4.8 线程的死锁 369 16.4.9 防止错误的使用wait、notify、notifyAll方法 371 16.5 获取当前正在...

    JAVA程序设计教程

    思考与练习.....................................................................................................................12 上机实习题..............................................................

Global site tag (gtag.js) - Google Analytics