Class AbstractRunnableBlockingQueueConsumer<E>

java.lang.Object
com.globalmentor.collections.AbstractRunnableBlockingQueueConsumer<E>
Type Parameters:
E - The type of elements held in the queue.
All Implemented Interfaces:
Consumer<E>, Runnable

public abstract class AbstractRunnableBlockingQueueConsumer<E> extends Object implements Consumer<E>, Runnable
A runnable consumer that takes elements from a blocking queue and consume them. Normally this consumer is run in a separate thread so that consuming can occur asynchronously. Each element is taken using BlockingQueue.take() and processed using consume(Object). This runnable's interruption policy is to end execution.
Author:
Garret Wilson
See Also:
  • Constructor Details

    • AbstractRunnableBlockingQueueConsumer

      public AbstractRunnableBlockingQueueConsumer(BlockingQueue<E> blockingQueue)
      Blocking queue constructor.
      Parameters:
      blockingQueue - The blocking queue from which elements will be consumed.
      Throws:
      NullPointerException - if the given blocking queue is null.
  • Method Details

    • getBlockingQueue

      protected BlockingQueue<E> getBlockingQueue()
      Returns:
      The blocking queue from which elements are being consumed.
    • run

      public void run()
      The main functionality of the consumer, which consumes data from the blocking queue and calls consume(Object).
      Specified by:
      run in interface Runnable
    • started

      protected void started()
      Called when the consumer is started before processing ends.
    • consume

      public abstract void consume(E element)
      Consumes an element from the queue.
      Specified by:
      consume in interface Consumer<E>
      Parameters:
      element - The element to consume.
    • stopped

      protected void stopped()
      Called when the consumer is stopped after processing ends.