com.thinkaurelius.titan.util.system

## Class BackgroundThread

- java.lang.Object
- - java.lang.Thread
  - - com.thinkaurelius.titan.util.system.BackgroundThread

- All Implemented Interfaces:
    Runnable

      public abstract class BackgroundThread
      extends Thread

  Author:
    Matthias Broecheler (me@matthiasb.com)

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class java.lang.Thread

      `Thread.State, Thread.UncaughtExceptionHandler`

  - ### Field Summary

    - ### Fields inherited from class java.lang.Thread

      `MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `BackgroundThread(String name, boolean daemon)` NEVER set daemon=true and override the cleanup() method.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected abstract void` | `action()` The action taken by this background thread when the wait condition is met.
    `protected void` | `cleanup()` Any clean up that needs to be done before this thread is closed down.
    `void` | `close(long maxWait, TimeUnit unit)`
    `void` | `run()`
    `protected abstract void` | `waitCondition()` The wait condition for the background thread.

    - ### Methods inherited from class java.lang.Thread

      `activeCount, checkAccess, clone, 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

      `equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### BackgroundThread

          public BackgroundThread(String name,
                          boolean daemon)

      NEVER set daemon=true and override the cleanup() method. If this is a daemon thread there is no guarantee that cleanup is called.
      Parameters:
        `name` -
        `daemon` -

  - ### Method Detail

    - #### run

          public void run()

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

    - #### waitCondition

          protected abstract void waitCondition()
                                         throws InterruptedException

      The wait condition for the background thread. This determines what this background thread is waiting for in its execution. This might be elapsing time or availability of resources. Since there is a wait involved, this method should throw an InterruptedException
      Throws:
        `InterruptedException`

    - #### action

          protected abstract void action()

      The action taken by this background thread when the wait condition is met. This action should execute swiftly to ensure that this thread can be closed in a reasonable amount of time. This action will not be interrupted by `close(long, TimeUnit)`.

    - #### cleanup

          protected void cleanup()

      Any clean up that needs to be done before this thread is closed down.

    - #### close

          public void close(long maxWait,
                   TimeUnit unit)

Copyright © 2012–2014. All rights reserved.