博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java_生产者消费者模式
阅读量:6120 次
发布时间:2019-06-21

本文共 3020 字,大约阅读时间需要 10 分钟。

/* * 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();    }}

转载于:https://www.cnblogs.com/MarchThree/p/3720415.html

你可能感兴趣的文章
IIS7如何显示详细错误信息
查看>>
C++文件读写详解(ofstream,ifstream,fstream)
查看>>
Android打包常见错误之Export aborted because fatal lint errors were found
查看>>
Tar打包、压缩与解压缩到指定目录的方法
查看>>
新手如何学习 jQuery?
查看>>
配置spring上下文
查看>>
Python异步IO --- 轻松管理10k+并发连接
查看>>
mysql-python模块编译问题解决
查看>>
Oracle中drop user和drop user cascade的区别
查看>>
【Linux】linux经常使用基本命令
查看>>
HTML模块化:使用HTML5 Boilerplate模板
查看>>
登记申请汇总
查看>>
Google最新截屏案例详解
查看>>
2015第31周日
查看>>
在使用EF开发时候,遇到 using 语句中使用的类型必须可隐式转换为“System.IDisposable“ 这个问题。...
查看>>
Oracle 如何提交手册Cluster Table事务
查看>>
BeagleBone Black第八课板:建立Eclipse编程环境
查看>>
在服务器上用Fiddler抓取HTTPS流量
查看>>
文件类似的推理 -- 超级本征值(super feature)
查看>>
【XCode7+iOS9】http网路连接请求、MKPinAnnotationView自定义图片和BitCode相关错误--备用...
查看>>