Being asynchronously notified of a BlockingQueue having an item available

前端 未结 3 817
隐瞒了意图╮
隐瞒了意图╮ 2021-02-19 18:22

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 p

相关标签:
3条回答
  • 2021-02-19 18:30

    This reply is too late on the topic, but recently I was working on a similar problem and this is what I used.

    When BlockingQueue receives a item/object, I used Spring's SimpleApplicationEventMulticaster which supports Asynchronous event processing. Basically, I published an event and configured it to process asynchronously rather than blocking the same execution thread, allowing the queue consumer to keep consuming as long as there are items to be processed in BlockingQueue, while the event listener/consumer can perform action asynchronously.

    0 讨论(0)
  • 2021-02-19 18:45

    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

    0 讨论(0)
  • 2021-02-19 18:51

    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);
    }
    
    0 讨论(0)
提交回复
热议问题