博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程简单实例(2)生产者和消费者
阅读量:4705 次
发布时间:2019-06-10

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

这是一个生产者和消费者的例子。消费者从仓库取物品,生产者向仓库增加商品。

当商品说达到最大时,不能继续增加商品,应该通知其他线程去取商品。

同样,当仓库商品为空时,无法取商品,而是通知其他线程增加商品。

这里用到线程的两个常用的方法:notifyAll和wait方法。

package code.thread;//Product and Custompublic class ProAndCus {    public static void main(String[] args) {        Store store = new Store(10);        Custom custom = new Custom(store);        Product product = new Product(store);        Custom custom2 = new Custom(store);        Product product2 = new Product(store);                custom.start();        product.start();        custom2.start();        product2.start();    }}//商品仓库class Store {    //最大储存商品数    private final int MAX_SIZE;    //当前商品数    private int count;        public Store(int size) {        MAX_SIZE = size;        count = 0;    }        //增加商品    public synchronized void add() {        if(count>=MAX_SIZE) {            System.out.println(Thread.currentThread().getName()+"  Store is full, please to get. count:"+count);            try{                this.wait();            }catch(InterruptedException e) {                e.printStackTrace();            }        }else{            System.out.println(Thread.currentThread().getName()+" custom :current count: "+count);            count++;            this.notifyAll();        }    }        //移除商品    public synchronized void remove() {        if(count<=0) {            System.out.println(Thread.currentThread().getName()+"  Store is empty,please input.  count:"+count);            try{                this.wait();            }catch(InterruptedException e){                e.printStackTrace();            }        }else{            count--;            System.out.println(Thread.currentThread().getName()+"  product: current count: "+count);            this.notifyAll();        }    }}//消费者class Custom extends Thread {    Store store;    public Custom(Store store) {        this.store = store;    }    @Override    public void run() {        while(true){            store.remove();            try {                Thread.sleep(1500);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}//生产者class Product extends Thread {    Store store;    public Product(Store store) {        this.store = store;    }        @Override    public void run() {        while(true){            store.add();            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}

 

转载于:https://www.cnblogs.com/justenjoy/p/7044821.html

你可能感兴趣的文章
数的计算
查看>>
java基础总结
查看>>
基于最小生成树的实时立体匹配算法简介
查看>>
MySQL 聚合函数 控制流程函数
查看>>
洛谷P2574 XOR的艺术
查看>>
jQuery操作字符串
查看>>
pyautogui 文档(二):鼠标控制
查看>>
ASP.NET Web API路由规则(二)
查看>>
怎么用mingw运行c程序?
查看>>
040 Android TCP协议的Socket通信
查看>>
KMP模板(HDU1711)
查看>>
企业级电商一般包括哪些模块总结
查看>>
研究下
查看>>
配置react+webpack+es6中的一些教训
查看>>
python中的内容编码
查看>>
Codefoeces 734F. Anton and School 数学
查看>>
斐波那契查找法
查看>>
java 22 - 23 多线程之定时器的概述和使用例子
查看>>
我们都爱大长腿
查看>>
实验10:Problem G: STL——多重集的插入和删除
查看>>