|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Thread
com.iparelan.util.PeriodicSuspendableThread
@Copyright(value="Copyright © 2008, Iparelan Solutions, LLC. All rights reserved.") public final class PeriodicSuspendableThread
A periodic background (daemon) task that can be suspended.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.lang.Thread |
---|
Thread.State, Thread.UncaughtExceptionHandler |
Field Summary | |
---|---|
static int |
DEFAULT_REFRESH_PERIOD
|
private static String |
DEFAULT_THREAD_NAME_PREFIX
|
private Condition |
disabledCondition
|
private boolean |
enabled
Decides whether this Thread is suspended/disabled or enabled. |
private Condition |
enabledCondition
|
private Runnable |
errorHook
|
private Lock |
lock
|
(package private) static Logger |
LOGGER
|
static String |
LOGGER_NAME
|
private static int |
MILLISECONDS_PER_SECOND
|
private int |
refreshPeriodMillis
|
private static AtomicLong |
serialNumberGenerator
|
private Runnable |
work
|
Fields inherited from class java.lang.Thread |
---|
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
Constructor Summary | |
---|---|
PeriodicSuspendableThread(Runnable work)
Creates a PeriodicSuspendableThreadImpl with a default name and
a default refresh period (5 seconds). |
|
PeriodicSuspendableThread(String name,
int refreshPeriod,
Runnable work,
Runnable errorHook)
Creates a PeriodicSuspendableThreadImpl . |
Method Summary | |
---|---|
void |
disable()
"Turns-off" this task. |
void |
enable()
"Turns-on" this task. |
private void |
ensureEnableCalled()
Barrier that waits until this thread is "turned-on." |
boolean |
isEnabled()
Tells whether this task is enabled. |
void |
run()
Repeatedly performs the work of the thread at periodic intervals. |
Methods inherited from class java.lang.Thread |
---|
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final String LOGGER_NAME
static final Logger LOGGER
public static final int DEFAULT_REFRESH_PERIOD
private static final int MILLISECONDS_PER_SECOND
private static final AtomicLong serialNumberGenerator
private static final String DEFAULT_THREAD_NAME_PREFIX
private final Lock lock
private final Condition enabledCondition
private final Condition disabledCondition
private final Runnable work
private final Runnable errorHook
private final int refreshPeriodMillis
private boolean enabled
Thread
is suspended/disabled or enabled.
Constructor Detail |
---|
public PeriodicSuspendableThread(Runnable work)
PeriodicSuspendableThreadImpl
with a default name and
a default refresh period (5 seconds).
work
- The Runnable
that specifies the work performed by this
PeriodicSuspendableThread
. May not be null
.
IllegalArgumentException
- Iff work
is null
.public PeriodicSuspendableThread(String name, int refreshPeriod, Runnable work, @Nullable Runnable errorHook)
PeriodicSuspendableThreadImpl
.
name
- The Thread
name, this parameter has the same
semantics and restrictions as that of Thread.Thread(String)
.refreshPeriod
- The refresh period in seconds.work
- The Runnable
that specifies the work performed
by this PeriodicSuspendableThread
. May not be null
.errorHook
- The Runnable
that specifies the policy to
invoke if work
terminates abruptly by throwing an
exception or raising an error other than InterruptedException
. In the case InterruptedException
, the current execution of work
is
stopped, but future executions are attempted. Any exceptions or
errors thrown by errorHook
are ignored. May be null
.
IllegalArgumentException
- Iff work
is null
.Method Detail |
---|
public boolean isEnabled()
SuspendableTask
isEnabled
in interface SuspendableTask
true
if this task is enabled, otherwise false
.public void enable()
SuspendableTask
SuspendableTask.disable()
before task execution begins may
stop task execution.
enable
in interface SuspendableTask
public void disable()
SuspendableTask
The task may not be turned off immediately if it does not respond to
the thread interruption scheme
, or
if it is not blocked.
disable
in interface SuspendableTask
private void ensureEnableCalled()
enable()
public void run()
enabled
for work to be performed.
Either the work or the pause may be interrupted. In such cases the
next pause or work iteration is performed, respectively. The work can
be interrupted if the code invoked by work.run()
satisfies the
conditions described by Thread.interrupt()
. If none of
these conditions hold, then the current work iteration cannot be
interrupted, in particular, the deprecated Thread.suspend()
is not invoked.
The pause duration is constant, save for interruption, in particular, it is not affected by the work duration.
run
in interface Runnable
run
in class Thread
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |