public abstract class DelayedAction extends Object implements Runnable
A "delayed action" is a some action that needs to get done by some time in the future.
This class does two things:
cancel() a future scheduled action, if any.
The action itself is defined by the subclass' implementation of run().
To avoid races, this class requires the user to supply a locking object. This may either be a normal
Java object, in which case normal Java synchronization is used, or a Lock object. The locking object is
used to serialize scheduling activity and action invocation. In other words, the locking object is locked during
the execution of schedule(), cancel(), and run().
Therefore, any time the locking object is locked, the state of this DelayedAction instance is "frozen"
in one of three states: not scheduled, scheduled, or executing (in the latter case, of course the thread doing the
executing is the one holding the lock). Therefore, to completely avoid race conditions, user code must itself
lock the locking object itself prior to invoking any methods in this class.
Typically the most convenient locking object to use is the user's own this object, which can be locked using a
synchronized method or block.
Note: in the case that run() invokes Object.wait() on the locking object, thereby
temporarily releasing the lock, to any other methods in this class it will appear as if that execution has already
completed.
| Modifier | Constructor and Description |
|---|---|
protected |
DelayedAction(Lock lock,
ScheduledExecutorService executorService)
Constructor utitilizing a
ScheduledExecutorService and a Lock for locking. |
protected |
DelayedAction(Lock lock,
TaskScheduler taskScheduler)
Constructor utitilizing a
TaskScheduler and a Lock for locking. |
protected |
DelayedAction(Object lock,
ScheduledExecutorService executorService)
Constructor utitilizing a
ScheduledExecutorService and normal Java object locking. |
protected |
DelayedAction(Object lock,
TaskScheduler taskScheduler)
Constructor utitilizing a
TaskScheduler and normal Java object locking. |
| Modifier and Type | Method and Description |
|---|---|
void |
cancel()
Cancel the future scheduled action, if any.
|
Date |
getScheduledTime()
Get the scheduled time for the outstanding scheduled action, if any.
|
boolean |
isScheduled()
Determine whether there is currently an outstanding scheduled action.
|
void |
schedule(Date date)
Schedule the delayed action for the given time.
|
protected ScheduledFuture<?> |
schedule(Runnable action,
Date date)
Schedule the given action using the task scheduler passed to the constructor.
|
protected DelayedAction(Object lock, TaskScheduler taskScheduler)
TaskScheduler and normal Java object locking.lock - locking object used to serialize activity, or null for thistaskScheduler - scheduler objectIllegalArgumentException - if taskScheduler is nullprotected DelayedAction(Object lock, ScheduledExecutorService executorService)
ScheduledExecutorService and normal Java object locking.lock - locking object used to serialize activity, or null for thisexecutorService - scheduler objectIllegalArgumentException - if executorService is nullprotected DelayedAction(Lock lock, TaskScheduler taskScheduler)
TaskScheduler and a Lock for locking.lock - locking object used to serialize activitytaskScheduler - scheduler objectIllegalArgumentException - if lock is nullIllegalArgumentException - if taskScheduler is nullprotected DelayedAction(Lock lock, ScheduledExecutorService executorService)
ScheduledExecutorService and a Lock for locking.lock - locking object used to serialize activityexecutorService - scheduler objectIllegalArgumentException - if lock is nullIllegalArgumentException - if executorService is nullpublic void schedule(Date date)
More precisely:
The net result is that, for any invocation, this method guarantees exactly one execution of the action will occur approximately on or before the given date; however, multiple invocations of this method prior to action execution can only ever result in a single "shared" action.
date - scheduled execution time (at the latest)IllegalArgumentException - if date is nullTaskRejectedException - if the given task was not accepted for internal reasons (e.g. a pool overload handling policy
or a pool shutdown in progress)public void cancel()
More precisely:
public boolean isScheduled()
public Date getScheduledTime()
protected ScheduledFuture<?> schedule(Runnable action, Date date)
action - action to performdate - when to perform itactionIllegalArgumentException - if either parameter is nullRejectedExecutionException - if the given task was not accepted for internal reasons (e.g. a pool overload handling
policy or a pool shutdown in progress)Copyright © 2020. All rights reserved.