多线程学习——消费者生产者(2)
2013-10-06 22:16
323 查看
package com.daicy.ProducerConsumer; import java.util.concurrent.LinkedBlockingQueue; public class DLinkedBlockingQueue<E> implements IDStack<E> { private LinkedBlockingQueue stack = new LinkedBlockingQueue(); @Override public E push(E item) throws InterruptedException { // TODO Auto-generated method stub stack.put(item); return item; } @Override public E pop() throws InterruptedException { // TODO Auto-generated method stub return (E) stack.take(); } }
package com.daicy.ProducerConsumer; import java.util.Stack; public class DStack<E> implements IDStack<E> { private Stack stack = new Stack(); public synchronized E push(E item) { stack.push(item); notify(); return item; } public synchronized E pop() throws InterruptedException { while (stack.size() == 0) { this.wait(); } return (E) stack.pop(); } }
package com.daicy.ProducerConsumer; public interface IDStack<E> { public E push(E item) throws InterruptedException; public E pop() throws InterruptedException; }
package com.daicy.ProducerConsumer; public class Mantou { private int id; Mantou(int id) { this.id = id; } public String toString() { return "Mantou :" + id; } }
package com.daicy.sequence; public class SafeSequence { private static int value; public static synchronized int getNext() { return value++; } }
相关文章推荐
- JAVA学习第二十七课(多线程(六))- 多生产者多消费者问题(JDK1.5新特性)
- java多线程学习生产者消费者
- 多线程学习(九)并发协作-生产者消费者模型
- 多线程学习——消费者生产者(1)
- 学习日记1105--多线程间的通信(生产者消费者)(1)
- Java多线程学习之 BlockingQueue实现生产者和消费者模型
- 多线程学习Demo注解(3)——生产者和消费者
- Java基础学习6_多线程(线程间通信--生产者消费者)
- Java多线程学习之生产者消费者问题(一)
- 训练营学习笔记 java多线程(五)生产者/消费者
- JAVA学习第二十七课(多线程(六))- 多生产者多消费者问题(JDK1.5新特性)
- 马士兵-多线程学习第05课 生产者消费者问题
- 学习多线程的生产者和消费者系列的相关实践
- C++多线程学习:生产者消费者问题
- Java基础学习__多线程(线程间通信--生产者消费者JDK5.0升级版)
- 9. 多线程 Part 4 生产者及消费者模式 --- 学习笔记
- C#多线程学习3—— 生产者和消费者
- java多线程学习之生产者与消费者(二)
- Linux C/C++多线程学习:生产者消费者问题
- java学习多线程之生产者消费者