Class RunnableQueues

java.lang.Object
com.kosdev.kos.commons.util.concurrent.RunnableQueues
All Implemented Interfaces:
Runnable

public class RunnableQueues extends Object implements Runnable
Object that allows other runnables to be added in queues or a pool. Runnables added to queues will be run in fifo ordering for the queue, although runnables across different queues may be run in any order and concurrently. Runnables added to the pool will be run in fifo order but can run concurrently in different threads.

RunnableQueues only holds runnables and dispatches them but it doesn't spawn any threads. RunnableQueues implements Runnable and calling the run() method will run all pending runnables. By calling run() from multiple concurrent threads, the pending runnables can be run concurrently subject to the constraints mentioned above. This allows the caller to control how the actual work is dispatched.

To use this, create a single instance, add a runnable to a queue and then run the RunnableQueues instance itself. If the queue is already busy, the run will exit immediately and the thread currently processing the queue will pick up the next runnable when it returns. This means there is no guarantee which thread will run a given queued runnable, but so long as the RunnableQueues instance is run after every addition, all runnables will be executed. This also ensures that the runnables will be run with maximum available concurrency.

Since:
1
Version:
1
  • Constructor Details

    • RunnableQueues

      public RunnableQueues()
  • Method Details

    • addToQueue

      public RunnableQueues addToQueue(Object queueKey, Runnable runnable)
      Add a runnable to a given queue. The queue is identified by an arbitrary key object. A reference to the key will be held until all the runnables in the queue have been run. If the key is null then the runnable will be added to the pool which has no concurrency constraints.
      Parameters:
      queueKey -
      runnable -
      Since:
      1
    • addToPool

      public RunnableQueues addToPool(Runnable runnable)
      Add a runnable to the pool. Runnables in the pool can be run concurrently and independently from queues. This equivalent to calling addToQueue() with a null queueKey.
      Since:
      1
    • run

      public void run()
      Run all the runnables in the various queues in the calling thread. This can be called concurrently from multiple threads and each thread will search the queue list for an available queue to process, exiting if no free queues are available or no runnables are left.
      Specified by:
      run in interface Runnable
      Since:
      1
    • waitForQueue

      public void waitForQueue(Object queueKey)
      Put a marker in the specified queue and wait until the marker is run. This ensures that all runnables in the specified queue will be run before this returns. It is possible that runnables in other channels may also be run in the process. The calling thread may actually run the pending runnables or it may simply block if a thread is already processing the queue.
      Since:
      1
    • schedule

      public void schedule()
      Add the RunnableQueue to the default KOS scheduler thread pool This is just a convenience method for: KosUtil.execute(runnableQueue) .
      Since:
      1