public class ProducerConsumer<T,R> extends Object
| Constructor and Description |
|---|
ProducerConsumer(int numThreads)
ProducerConsumer maintains input and output queues and a thread-pool of
workers.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addWorker(WorkRequestProcessor<T,R> processor)
Add another worker that will consume WorkRequest<T> items
from input queue, process each item using supplied processor, and for
every processed item output WorkReport<R> to output queue.
|
WorkReport<R> |
blockingTake()
Blocking take from ProducerConsumer output queue (catches exceptions and
retries forever).
|
int |
getWorkCnt()
Returns number of pending ProducerConsumer items (submitted to input
queue for processing via put() method but not yet consumed by take()
or blockingTake().
|
boolean |
hasWork()
Returns true if there are items in ProducerConsumer that are either
pending for processing or waiting to be consumed.
|
void |
put(WorkRequest<T> workRequest)
Blocking put workRequest to ProducerConsumer input queue.
|
void |
shutdown()
Shutdown ProducerConsumer worker thread-pool without waiting for
completion of any pending work.
|
WorkReport<R> |
take()
Blocking take from ProducerConsumer output queue that can be interrupted.
|
public ProducerConsumer(int numThreads)
numThreads - Size of thread-pool to execute Workers.public void addWorker(WorkRequestProcessor<T,R> processor)
processor - Processor implementing WorkRequestProcessor interface.public void shutdown()
public int getWorkCnt()
public boolean hasWork()
public void put(WorkRequest<T> workRequest)
workRequest - item to be processed.public WorkReport<R> take() throws InterruptedException
InterruptedException - if interrupted before an element becomes
available.public WorkReport<R> blockingTake()
Copyright © 2008–2020 Apache Software Foundation. All rights reserved.