/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testdemo1; import java.util.logging.Level;
import java.util.logging.Logger; class Mantou { private int id; public Mantou(int val) {
this.id = val;
} public String toString() {
return "Mantou id: " + this.id;
}
} class Basket { private Mantou[] mStack = new Mantou[6];
private int index = 0; public synchronized void push(Mantou m) {
while (index == mStack.length) {
try {
this.wait();
System.out.println("full........................!!!"); } catch (InterruptedException ex) {
Logger.getLogger(Basket.class.getName()).log(Level.SEVERE, null, ex);
}
}
this.notify(); mStack[index] = m; index++; System.out.println("add Mantou id= " + m + " and index= " + index); } public synchronized Mantou pop() { while (index == 0) { try {
System.out.println("empty........................!!!");
wait();
} catch (InterruptedException ex) {
Logger.getLogger(Basket.class.getName()).log(Level.SEVERE, null, ex);
}
}
this.notify();
index--;
Mantou m = mStack[index];
System.out.println("get Mantou id= " + m + " and index= " + index); return m;
}
} class Producer implements Runnable { private Basket brasket = new Basket(); public Producer(Basket val) {
this.brasket = val;
} @Override
public void run() {
for (int i = 0; i < 20; i++) {
Mantou m = new Mantou(i);
brasket.push(m); try {
// Thread.sleep(Math);
Thread.sleep((int) (Math.random() * 500));
} catch (InterruptedException ex) {
Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
} }
} } class Consumer implements Runnable { private Basket brasket = new Basket(); public Consumer(Basket val) {
this.brasket = val;
} @Override
public void run() {
for (int i = 0; i < 20; i++) {
Mantou m = brasket.pop();
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} public class TestDemo1 { public static void main(String[] args) {
// TODO code application logic here
Basket s = new Basket();
Producer producer = new Producer(s);
Consumer consumer = new Consumer(s);
Thread tr1 = new Thread(producer);
Thread tr2 = new Thread(consumer);
tr1.start();
tr2.start();
} }

Java_生产者消费者模式的更多相关文章

  1. java多线程 生产者消费者模式

    package de.bvb; /** * 生产者消费者模式 * 通过 wait() 和 notify() 通信方法实现 * */ public class Test1 { public static ...

  2. LabVIEW之生产者/消费者模式--队列操作 彭会锋

    LabVIEW之生产者/消费者模式--队列操作 彭会锋 本文章主要是对学习LabVIEW之生产者/消费者模式的学习笔记,其中涉及到同步控制技术-队列.事件.状态机.生产者-消费者模式,这几种技术在在本 ...

  3. 转:Task任务调度实现生产者消费者模式 (个人理解后文)

    纯属个人愚见.欢迎加入反驳(PiDou). 1.前文大致就是,利用Queue配置的一个TaskFactory任务调度器.实现生产者消费者模式的例子..首先我就试了 第一种 FIFO(先进先出)的配置. ...

  4. Lucene.net站内搜索—4、搜索引擎第一版技术储备(简单介绍Log4Net、生产者消费者模式)

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  5. MVC异常日志生产者消费者模式记录(异常过滤器)

    生产者消费者模式 定义自己的异常过滤器并注册 namespace Eco.Web.App.Models { public class MyExceptionAttribute : HandleErro ...

  6. 转:Task任务调度实现生产者消费者模式

    我们经常会遇到生产者消费者模式,比如前端各种UI操作事件触发后台逻辑等.在这种典型的应用场景中,我们可能会有4个业务处理逻辑(下文以P代表生产者,C代表消费者): 1. FIFO(先进先出)      ...

  7. .net学习之多线程、线程死锁、线程通信 生产者消费者模式、委托的简单使用、GDI(图形设计接口)常用的方法

    1.多线程简单使用(1)进程是不执行代码的,执行代码的是线程,一个进程默认有一个线程(2)线程默认情况下都是前台线程,要所有的前台线程退出以后程序才会退出,进程里默认的线程我们叫做主线程或者叫做UI线 ...

  8. 使用BlockingQueue的生产者消费者模式

    BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利.使用场景. 首先它是一个队列,而一个队 ...

  9. Java设计模式—生产者消费者模式(阻塞队列实现)

    生产者消费者模式是并发.多线程编程中经典的设计模式,生产者和消费者通过分离的执行工作解耦,简化了开发模式,生产者和消费者可以以不同的速度生产和消费数据.这篇文章我们来看看什么是生产者消费者模式,这个问 ...

随机推荐

  1. 【jQuery】jQuery操作<input>的聚焦与全选其内容

    实现效果: 源代码: $(function() { $("#exist_code_remind").attr("style","display:non ...

  2. Jquery AutoComplete的使用方法实例

    jquery.autocomplete详解 语法: autocomplete(urlor data, [options] ) 参数: url or data:数组或者url [options]:可选项 ...

  3. SpannableStringBuilder 和 SpannableString

    EditText:         通常用于显示文字,但有时候也需要在文字中夹杂一些图片,比如QQ中就可以使用表情图片,又比如需要的文字高亮显示等等,如何在android中也做到这样呢? 记得andr ...

  4. Mongo DB 安装-及分布式集群部署(初稿)

    一.安装步骤, 1, 下载最新的Mongo DB数据库:http://www.mongodb.org/downloads?_ga=1.44426535.2020731121.1421844747\ 下 ...

  5. 关于在MDK中使用 printf 函数

    microlib 提供了一个有限的 stdio 子系统,它仅支持未缓冲的 stdin.stdout 和 stderr. 这样,即可使用 printf() 来显示应用程序中的诊断消息. 要使用高级 I/ ...

  6. HDU2015校赛 The Country List

    今天手感真差..各种读错题意.水题... 就是说,给你几个串.如果长度一样并且相同位置字符相同(不分大小写)的个数大于两个就是不同串. #include<iostream> #includ ...

  7. HW6.20

    public class Solution { public static void main(String[] args) { int[][] chessboard = new int[8][8]; ...

  8. MySQL 字段类型详解

    一.非数字类型 类型 范围 说明   Char(N) [ binary] N=1~255 个字元 binary :分辨大小写 固定长度 std_name cahr(32) not null VarCh ...

  9. hdu5773--The All-purpose Zero(LIS变形)

    题意:给一个非负整数的数列,其中0可以变成任意整数,包括负数,求最长上升子序列的长度. 题解:LIS是最简单的DP了,但是变形之后T^T真的没想到.数据范围是10^5,只能O(nlogn)的做法,所以 ...

  10. Activity详解

    Activity是android应用的重要组成单元之一(另外3个是Service,BroadcastReceiver和ContentProvider).实际应用包含了多个Activity,不同的Act ...