Class ThreadedWorkQueue<W>

  • Type Parameters:
    W -

    public class ThreadedWorkQueue<W>
    extends java.lang.Object
    This class implements a work queue of pooled objects. You can offer a stream of objects to the backing poool of threads and it will consume them and hand them to the BatchWorker as a collection to be processed (batched).

    Essentially, it uses BatchWorker as Callable/Future with a collection argument.

    Author:
    cschanck
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  ThreadedWorkQueue.BatchWorker<WW>
      Callback class, think of it as a Runnable with an argument that is a Collection.
    • Constructor Summary

      Constructors 
      Constructor Description
      ThreadedWorkQueue​(ThreadedWorkQueue.BatchWorker<W> dispatcher, int numberOfThreads, java.util.concurrent.ThreadFactory factory, int maximumQueueSize, int batchSize)
      Create a work queue where work is dispatched through the given dispatcher, which the specified number of threads.
    • Constructor Detail

      • ThreadedWorkQueue

        public ThreadedWorkQueue​(ThreadedWorkQueue.BatchWorker<W> dispatcher,
                                 int numberOfThreads,
                                 java.util.concurrent.ThreadFactory factory,
                                 int maximumQueueSize,
                                 int batchSize)
        Create a work queue where work is dispatched through the given dispatcher, which the specified number of threads.
        Parameters:
        dispatcher - Thread safe dispatcher to use to dispatch work
        numberOfThreads - Number of parallel threads used process work from this queue
        factory - ThreadFactory used to create the threads
        maximumQueueSize - maximum backlog of work items that can be queued before items get dropped
        batchSize - number of items, at a maximum, to send to a dispatcher at a time.
    • Method Detail

      • offer

        public void offer​(W workUnit)
        Offer a work unit to queue. Might push prior work units off of the work queue, dropped forever.
        Parameters:
        workUnit -
      • isAlive

        public boolean isAlive()
        Is this work queue still accepting work.
        Returns:
        true if still alive
      • getBacklogCount

        public long getBacklogCount()
        Get the current backlog count. An approximation, by necessity.
        Returns:
        count of items yet to be processed.
      • getOfferedCount

        public int getOfferedCount()
        Gets offer counter. Cumulative tripped
        Returns:
        the offer counter
      • getDroppedCount

        public int getDroppedCount()
        Gets dropped counter.
        Returns:
        the dropped counter
      • getProcessedCount

        public int getProcessedCount()
        Gets processed count.
        Returns:
        the processed count
      • getBatchSize

        public int getBatchSize()
        Get the batch size
        Returns:
        batch size