生产者与消费者
来源:互联网 发布:sql code 803 编辑:程序博客网 时间:2024/05/23 13:36
//生产者与消费者public class SyschTest { public static void main(String[] args) { Stack stack = new Stack("stack"); Producer p = new Producer(stack, "p"); Consumer c = new Consumer(stack, "c"); }}class Stack { private static String name; private static String buffer[] = new String[100]; private static int point = -1; public Stack(String name) { this.name = name; } public String getName() { return name; } public int getPoint() { return point; } public int size() { return point+1; } //入栈 public synchronized void push(String goods) { point ++ ; Thread.yield(); buffer[point] = goods ; } //出栈 public synchronized String pop() { if(point == -1) { return null; } String goods = buffer[point]; buffer[point] = null; Thread.yield(); point --; return goods; }}//生产者class Producer extends Thread{ private String name ; private Stack theStack; public Producer(Stack s,String name) { this.name = name; this.theStack = s; start(); } public void run(){ String goods = null ; for(int i =0;i<20;i++){ goods = "goods"+ (theStack.getPoint()+1); theStack.push(goods); System.out.println(name + " push "+ goods +" into "+theStack.getName()); yield(); } }} //消费者class Consumer extends Thread { private String name ; private Stack theStack; public Consumer(Stack s,String name){ this.theStack = s; this.name = name; start(); } public void run(){ String goods = null; for(int i=0;i<20;i++) { goods = theStack.pop(); System.out.println(name +" pop " + goods + " from " +theStack.getName()); yield(); } }}
0 0
- 生产者与消费者问题
- 生产者与消费者
- 生产者与消费者
- 生产者与消费者问题
- 消费者与生产者模式
- 消费者与生产者模式
- 消费者与生产者模式
- 生产者与消费者问题
- 生产者与消费者
- 生产者与消费者
- 生产者与消费者
- 生产者与消费者问题
- C# 生产者与消费者
- 生产者与消费者
- 生产者与消费者--一对一
- 死锁,生产者与消费者
- 生产者与消费者模型
- java 生产者与消费者
- 文章标题
- <C/C++面试>华为13/14年校园招聘机试题
- 毕业后五年之内将决定你的一生
- 面向对象 Object与内部类
- 网络探测工具(二)——traceroute
- 生产者与消费者
- 如何用U盘制作Ubuntu 14.10启动盘
- CocoaPods安装和使用教程
- VS2013下 qt5容易遇到的问题
- 构造与销毁顺序
- 重庆网络投票公司
- cocos2dx概要及开发技术—认识cocos2d-x游戏引擎
- java实现而叉树
- SQLite数据库