game.net
Class StoppableThread

java.lang.Object
  extended by java.lang.Thread
      extended by game.net.StoppableThread
All Implemented Interfaces:
java.lang.Runnable

public class StoppableThread
extends java.lang.Thread

A Thread you can stop gracefully. Remember nullify references to the thread after you are finished. Threads are big. You can't restart it! version 1.0 2002-08-01 version 1.1 2002-08-02 Make pleaseStop volatile and synchronize access to it. Required for multi-cpu cache synchronization.

Author:
Roedy Green

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
StoppableThread(java.lang.Runnable r)
          Constructor.
 
Method Summary
 void gentleStop(boolean interrupt, long timeout)
          Stop this thread gracefully.
 void start()
          Start this thread executing its run method on a separate thread.
 boolean stopping()
          this.run should call stopping() at convenient invervals to see if it has been requested to stop.
 
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, run, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StoppableThread

public StoppableThread(java.lang.Runnable r)
Constructor.

Parameters:
r - Class that has a run method
Method Detail

gentleStop

public void gentleStop(boolean interrupt,
                       long timeout)
Stop this thread gracefully. If the thread is already stopped, does nothing. For this to work, this.run must exit by checking stopping() at convenient intervals and returning if it is true.

Parameters:
interrupt - true if this thread should be interrupted from sleep or from doing an i/o (which might close the channel), before stopping it.
timeout - How long in milliseconds to wait for the thread to die before giving up. 0 means wait forever. -1 means don't wait at all.

start

public void start()
Start this thread executing its run method on a separate thread. You may only call start once. After the thread dies it cannot be restarted.

Overrides:
start in class java.lang.Thread
Throws:
java.lang.IllegalThreadStateException - if this thread is already started.

stopping

public final boolean stopping()
this.run should call stopping() at convenient invervals to see if it has been requested to stop. If true, it should finish up quickly and return. Will have to be called via while ( ! ((StoppableThead)Thread.currentThread()).stopping() )

Returns:
true if run should exit soon.