com.iparelan.util
Class PeriodicSuspendableThread

java.lang.Object
  extended by java.lang.Thread
      extended by com.iparelan.util.PeriodicSuspendableThread
All Implemented Interfaces:
SuspendableTask, Runnable

@Copyright(value="Copyright © 2008, Iparelan Solutions, LLC. All rights reserved.")
public final class PeriodicSuspendableThread
extends Thread
implements SuspendableTask

A periodic background (daemon) task that can be suspended.

Version:
July 2008
Author:
Greg Mattes

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

LOGGER_NAME

public static final String LOGGER_NAME
See Also:
Constant Field Values

LOGGER

static final Logger LOGGER

DEFAULT_REFRESH_PERIOD

public static final int DEFAULT_REFRESH_PERIOD
See Also:
Constant Field Values

MILLISECONDS_PER_SECOND

private static final int MILLISECONDS_PER_SECOND
See Also:
Constant Field Values

serialNumberGenerator

private static final AtomicLong serialNumberGenerator

DEFAULT_THREAD_NAME_PREFIX

private static final String DEFAULT_THREAD_NAME_PREFIX
See Also:
Constant Field Values

lock

private final Lock lock

enabledCondition

private final Condition enabledCondition

disabledCondition

private final Condition disabledCondition

work

private final Runnable work

errorHook

private final Runnable errorHook

refreshPeriodMillis

private final int refreshPeriodMillis

enabled

private boolean enabled
Decides whether this Thread is suspended/disabled or enabled.

Constructor Detail

PeriodicSuspendableThread

public PeriodicSuspendableThread(Runnable work)
Creates a PeriodicSuspendableThreadImpl with a default name and a default refresh period (5 seconds).

Parameters:
work - The Runnable that specifies the work performed by this PeriodicSuspendableThread. May not be null.
Throws:
IllegalArgumentException - Iff work is null.

PeriodicSuspendableThread

public PeriodicSuspendableThread(String name,
                                 int refreshPeriod,
                                 Runnable work,
                                 @Nullable
                                 Runnable errorHook)
Creates a PeriodicSuspendableThreadImpl.

Parameters:
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.
Throws:
IllegalArgumentException - Iff work is null.
Method Detail

isEnabled

public boolean isEnabled()
Description copied from interface: SuspendableTask
Tells whether this task is enabled.

Specified by:
isEnabled in interface SuspendableTask
Returns:
true if this task is enabled, otherwise false.

enable

public void enable()
Description copied from interface: SuspendableTask
"Turns-on" this task. Calling this method is not guaranteed to commence task execution. Calling SuspendableTask.disable() before task execution begins may stop task execution.

Specified by:
enable in interface SuspendableTask

disable

public void disable()
Description copied from interface: SuspendableTask
"Turns-off" this task.

The task may not be turned off immediately if it does not respond to the thread interruption scheme, or if it is not blocked.

Specified by:
disable in interface SuspendableTask

ensureEnableCalled

private void ensureEnableCalled()
Barrier that waits until this thread is "turned-on."

See Also:
enable()

run

public void run()
Repeatedly performs the work of the thread at periodic intervals. A pause is taken after each iteration of work. The thread must be 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.

Specified by:
run in interface Runnable
Overrides:
run in class Thread


Copyright © 2008 Iparelan Solutions, LLC. All rights reserved.