多线程学习——消费者生产者(2)
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++;
}
}
如果你喜欢本文, 请长按二维码,关注公众号 分布式编程.
作者:分布式编程
出处:https://zthinker.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。