|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.curjent.impl.agent.ExpirationsTask
final class ExpirationsTask
Background task for expiring messages. Uses the controller's executor
(Messengers.getExecutor()
) to run the task. The task only runs when
there are timers to process.
Field Summary | |
---|---|
private Controller |
controller
Owning controller. |
private ArrayList<ExpirationsTimer> |
feed
New timers from the controller are queued in a simple list and processed in the background. |
private Condition |
monitor
Synchronization monitor for timer feeds. |
private PriorityBlockingQueue<ExpirationsTimer> |
queue
Ordered queue of timers. |
private AtomicBoolean |
running
Starting, stopping, and restarting the background task is coordinated using AtomicBoolean.compareAndSet(boolean, boolean) . |
private ExpirationsTimer[] |
timers
Enqueued timers from feed are copied to this buffer while the
controller is locked so that they can be processed outside of the lock. |
Constructor Summary | |
---|---|
ExpirationsTask(Controller controller,
ReentrantLock lock)
Saves the controller and its lock for internal use. |
Method Summary | |
---|---|
private boolean |
awaitFeed(long timeout)
Waits for new timers to queue, or for the given timeout period. |
(package private) void |
enqueue(ExpirationsTimer timer)
Enqueues a new timer for expiration. |
private long |
processQueue()
Dequeues and expires messages. |
void |
run()
Runs the background task for as long as there are more timers to process. |
(package private) void |
signal()
Notifies the background task that a message has finished. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private final Controller controller
private final Condition monitor
feed
private final AtomicBoolean running
AtomicBoolean.compareAndSet(boolean, boolean)
.
private final ArrayList<ExpirationsTimer> feed
timers
, but
those timers are added to queue
in the background without
locking the controller.
private ExpirationsTimer[] timers
feed
are copied to this buffer while the
controller is locked so that they can be processed outside of the lock.
The awaitFeed(long)
expands this buffer as needed.
Thread safety is ensured by updating the buffer while the controller is locked and only reading from it when unlocked. Reading is safe since only one background executor thread accesses the buffer at a time. The JVM memory model ensures consistency across threads used by the controller's executor.
private final PriorityBlockingQueue<ExpirationsTimer> queue
feed
.
Constructor Detail |
---|
ExpirationsTask(Controller controller, ReentrantLock lock)
Method Detail |
---|
void enqueue(ExpirationsTimer timer)
Called while the controller is locked.
void signal()
Called while the controller is locked.
public void run()
run
in interface Runnable
private long processQueue()
Long.MAX_VALUE
if the queue is empty.
private boolean awaitFeed(long timeout) throws InterruptedException
monitor
. The
timeout is specified by processQueue()
and denotes how long the
task should wait to expire the next message. A timeout value of
Long.MAX_VALUE
means the priority queue is empty.
Returns true
if there are more timers in the queue to
process. Sets running
to false
and returns the same
if there are no more timers, and the task should exit.
InterruptedException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |