Being asynchronously notified of a BlockingQueue having an item available

大憨熊 提交于 2019-12-05 18:27:59

问题


I need an Object to be asynchronously notified when some BlockingQueue has got an item to give.

I've searched both Javadoc and the web for a pre-made solution, then I ended up with a (maybe naive) solution of mine, here it is:

interface QueueWaiterListener<T> {
    public void itemAvailable(T item, Object cookie);
}

and

class QueueWaiter<T> extends Thread {

    protected final BlockingQueue<T> queue;
    protected final QueueWaiterListener<T> listener;
    protected final Object cookie;

    public QueueWaiter(BlockingQueue<T> queue, QueueWaiterListener<T> listener, Object cookie) {
        this.queue = queue;
        this.listener = listener;
        this.cookie = cookie;
    }

    public QueueWaiter(BlockingQueue<T> queue, QueueWaiterListener<T> listener) {
        this.queue = queue;
        this.listener = listener;
        this.cookie = null;
    }

    @Override
    public void run() {
        while (!isInterrupted()) {
            try {
                T item = queue.take();
                listener.itemAvailable(item, cookie);
            } catch (InterruptedException e) {
            }
        }
    }
}

Basically, there's a thread blocking on a take() operation of a queue that callbacks a listener object everytime a take() operation succeeds, optionally sending back a special cookie object (ignore it if you want).

Question is: is there any better way to do this? Am I doing some unforgivable mistake (both in concurrency/efficiency and/or code cleanness)? Thanks in advance.


回答1:


Perhaps you could subclass some BlockingQueue (such as ArrayBlockingQueue or LinkedBlockingQueue or what ever you're using), add support for listeners and do

@Override
public boolean add(E o) {
    super.add(o);
    notifyListeners(o);
}



回答2:


This looks like a good standard pattern for queue blocking and listeners. You make a good choice of making the listener interface. If you are not using the BlockingQueue class (which I am not clear of), the only thing is that you have manage is the correct wait() and notify() for managing the blocking call.

This particular SO Question "A simple scenario using wait() and notify() in java" provides a good overview on wait and notify and the usage related to BlockingQueue



来源:https://stackoverflow.com/questions/7318166/being-asynchronously-notified-of-a-blockingqueue-having-an-item-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!