T
- Type of items contained in the queue to be consumedpublic class BlockingQueueConsumer<T> extends AbstractService
BlockingQueue
so that items are only consumed as they become available. At the same time it has a
BlockingQueueConsumer.ConsumerAcceptor
that will only accept items as it is ready. By accepting on the same
thread as the consumer it will only try to take more items after the
BlockingQueueConsumer.ConsumerAcceptor.acceptConsumedItem(Object)
call returns.
Another way to look at it, this class provides the thread to handle blocking when consuming
from a BlockingQueue
.
Keep in mind that this class in no way attempts to solve the problem if the program is producing faster than the consumer accepts. In those conditions the queue will still continue to grow, and consume memory.
Modifier and Type | Class and Description |
---|---|
static interface |
BlockingQueueConsumer.ConsumerAcceptor<T>
Interface for an implementation which can accept consumed tasks.
|
Constructor and Description |
---|
BlockingQueueConsumer(java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.BlockingQueue<? extends T> queue,
BlockingQueueConsumer.ConsumerAcceptor<? super T> acceptor)
Constructs a new consumer, with a provided queue to consume from, and an acceptor to accept
items.
|
BlockingQueueConsumer(java.util.concurrent.ThreadFactory threadFactory,
java.lang.String threadName,
java.util.concurrent.BlockingQueue<? extends T> queue,
BlockingQueueConsumer.ConsumerAcceptor<? super T> acceptor)
Constructs a new consumer, with a provided queue to consume from, and an acceptor to accept
items.
|
hasStopped, isRunning, start, startIfNotStarted, stop, stopIfRunning
public BlockingQueueConsumer(java.util.concurrent.ThreadFactory threadFactory, java.util.concurrent.BlockingQueue<? extends T> queue, BlockingQueueConsumer.ConsumerAcceptor<? super T> acceptor)
threadFactory
- ThreadFactory to construct new thread for consumer to run onqueue
- queue to consume fromacceptor
- acceptor to provide consumed items topublic BlockingQueueConsumer(java.util.concurrent.ThreadFactory threadFactory, java.lang.String threadName, java.util.concurrent.BlockingQueue<? extends T> queue, BlockingQueueConsumer.ConsumerAcceptor<? super T> acceptor)
threadFactory
- ThreadFactory to construct new thread for consumer to run onthreadName
- Name of thread consumer runs on, or null
to generate a default onequeue
- queue to consume fromacceptor
- acceptor to provide consumed items to