Class RunnableQueues
- All Implemented Interfaces:
Runnable
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdd a runnable to the pool.addToQueue(Object queueKey, Runnable runnable) Add a runnable to a given queue.voidrun()Run all the runnables in the various queues in the calling thread.voidschedule()Add theRunnableQueueto the default KOS scheduler thread pool This is just a convenience method for:KosUtil.execute(runnableQueue).voidwaitForQueue(Object queueKey) Put a marker in the specified queue and wait until the marker is run.
-
Constructor Details
-
RunnableQueues
public RunnableQueues()
-
-
Method Details
-
addToQueue
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
Add a runnable to the pool. Runnables in the pool can be run concurrently and independently from queues. This equivalent to callingaddToQueue()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. -
waitForQueue
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 theRunnableQueueto the default KOS scheduler thread pool This is just a convenience method for:KosUtil.execute(runnableQueue).- Since:
- 1
-