博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 多线程四
阅读量:7227 次
发布时间:2019-06-29

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

 

一个生产者,消费者的例子:

import java.util.Stack;/** * Created by root on 17-10-1. */public class Test5 {    //商品:APPLE    class Apple {        private int id;        public Apple(int id) {            this.id = id;        }        @Override        public String toString() {            return "Apple{" +                    "id=" + id +                    '}';        }    }    //存放商品的仓库    class Repositorie {        Stack
capacity = new Stack<>(); int MAXSIZE = 10; public synchronized void putApple(Apple apple) throws InterruptedException { if (capacity.size() < MAXSIZE) { capacity.push(apple); System.out.println(Thread.currentThread().getName()+"生产Apple>>>" + apple.toString()); notify(); } else { wait(); } } public synchronized void getApple() throws InterruptedException { if (capacity.size() > 0) { System.out.println(" "+Thread.currentThread().getName()+"消费Apple<<<" + capacity.pop().toString()); notify(); } else { wait(); } } public void close(){ while (true){ //卖完了就打烊 if (capacity.size()==0){ System.exit(0); } } } } //生产者 class Producer implements Runnable { int NUM=20;//一共生产20个商品 private Repositorie repositorie; public Producer(Repositorie repositorie) { this.repositorie = repositorie; } @Override public void run() { for (; NUM>0; ) { try { repositorie.putApple(new Apple(NUM--)); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } repositorie.close(); } } //消费者 class Consume implements Runnable { private Repositorie repositorie; public Consume(Repositorie repositorie) { this.repositorie = repositorie; } @Override public void run() { //不知道有多少商品 for (;true;) { try { repositorie.getApple(); Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { Test5 test5 = new Test5(); Repositorie repositorie = test5.new Repositorie(); Producer producer = test5.new Producer(repositorie); Consume consume = test5.new Consume(repositorie); Thread p1 = new Thread(producer,"工人1"); Thread p2 = new Thread(producer,"工人2"); Thread c1 = new Thread(consume,"客户1"); Thread c2 = new Thread(consume,"客户2"); p1.start(); p2.start(); c1.start(); c2.start(); }}

结果:

工人1生产Apple>>>Apple{id=20}工人2生产Apple>>>Apple{id=19}      客户2消费Apple<<
>>Apple{id=18}工人2生产Apple>>>Apple{id=17}工人1生产Apple>>>Apple{id=16}工人2生产Apple>>>Apple{id=15}工人1生产Apple>>>Apple{id=14} 客户1消费Apple<<
>>Apple{id=13}工人1生产Apple>>>Apple{id=12}工人2生产Apple>>>Apple{id=11}工人1生产Apple>>>Apple{id=10}工人2生产Apple>>>Apple{id=9}工人1生产Apple>>>Apple{id=8} 客户1消费Apple<<
>>Apple{id=7}工人1生产Apple>>>Apple{id=6}工人2生产Apple>>>Apple{id=5} 客户1消费Apple<<
>>Apple{id=2}工人2生产Apple>>>Apple{id=1} 客户1消费Apple<<

 

转载于:https://www.cnblogs.com/lanqie/p/7617644.html

你可能感兴趣的文章
标准ACL+扩展ACL+命名ACL
查看>>
Meteor应用的启动过程分析
查看>>
九曲黄河万里沙,浪淘风簸自天涯 — 正则表达式
查看>>
欲哭无泪,联想笔记本性价比
查看>>
很简单的在Ubuntu系统下安装字体和切换默认字体的方法
查看>>
我的友情链接
查看>>
dojo框架用hitch实现函数与上下文的绑定
查看>>
ubuntu编译安装vim7.4
查看>>
python之利用PIL库实现页面的图片验证码及缩略图
查看>>
IP-COM设置×××
查看>>
VPC配置案例
查看>>
十年IT运维谈(五):要专业化还是平台化?
查看>>
分享超级给力的一个外发光Shader
查看>>
oblog_4.6_SQL 语句
查看>>
通过Git WebHooks+脚本实现自动更新发布代码之shell脚本
查看>>
对象实例化、字符串的使用方法
查看>>
keepalived基于LVS实现高可用,实现web服务的高可用
查看>>
80端口被Microsoft-HTTPAPI/2.0占用的解决办法
查看>>
无法抗拒Minecraft给予超高的自由度和探索-微访谈
查看>>
数据结构之串
查看>>