Java 阻塞队列实现

来源:互联网 发布:网络贷款 预防诈骗 编辑:程序博客网 时间:2024/05/16 23:42

阻塞队列概念:

阻塞队列与普通队列的区别是:当阻塞队列为空的时候,从队列取数会被阻塞,直到队列中有数线程才会被唤醒;如果队列已满,往队列存数线程也会被阻塞,直到队列非满线程才会被唤醒。

简单的阻塞队列实现有:

public class SimpleBlockQueue {    private int size = 0;    private List<Object> list = null;    public SimpleBlockQueue(int size){        this.size = size;        list = new ArrayList<Object>();    }    public synchronized void putValue(Object value) throws InterruptedException {        while(list.size() == size){            wait();        }                notifyAll();        list.add(value);    }    public synchronized void rmValue(Object value) throws InterruptedException {        while(list.size() == 0){            wait();        }        notifyAll();        list.remove(value);    }}


0 0