nl.knowledgeplaza.util
Class ThreadUtil

java.lang.Object
  extended by nl.knowledgeplaza.util.ThreadUtil

public class ThreadUtil
extends java.lang.Object

This class provides a thread local stop flag, which a thread may query to see if it needs to stop. Aside for the stopflag it also implements a thread related store and some utility methods.

Explanation

In multithreading environemts the advised approach to stopping a thread is making it exit the run method on its own. The Thread.stop has been depricated in order to support this. This means that basically every thread must monitor a stop flag in order to know when it is suppose to stop. Implementing this is a simple and repeating task and is done for you in this class.

In a thread simply call ThreadUtil.getStopFlag() (or mustStop or mayContinueToRun) to get the current thread's stop flag. If another thread wants thead X to stop, it simply calls ThreadUtil.setStopFlag(X, true). The stop flag for thread X is set to true and when it calls its getStopFlag, it will know it is supposed to stop cleanly.

The store automatically cleans when a thread is discarded. However thread pools are often used and then the user is responsible for makig sure the store contains correct data.

Version:
$Revision: 1.2 $

Field Summary
static java.lang.String SOURCECODE_VERSION
          Standard variable for determining version of a class file.
 
Constructor Summary
ThreadUtil()
           
 
Method Summary
static java.lang.Object get(java.lang.Runnable runnable, java.lang.String key)
          fetch a specific value for this thread
static java.lang.Object get(java.lang.String key)
           
static java.lang.Object get(java.lang.ThreadGroup runnable, java.lang.String key)
          fetch a specific value for this thread
static java.lang.String getStackTrace()
          get the stack trace of the current thread as a String
static boolean getStopFlag()
          query the stop flag for the current thread
static boolean getStopFlag(java.lang.Runnable pRunnable)
          query the stop flag for the specified Thread/Runnable
static void main(java.lang.String[] args)
           
static boolean mayContinue()
          may the current thread continue
static boolean mustStop()
          should the current thread stop
static java.lang.Object remove(java.lang.Runnable runnable, java.lang.String key)
          Remove a specific value for this thread
static java.lang.Object remove(java.lang.String key)
           
static void remove(java.lang.ThreadGroup runnable, java.lang.String key)
          Remove a specific value for this thread
static java.lang.Thread replaceThread(java.lang.Thread originalThread, java.lang.String name, java.lang.Runnable newRunnable)
          Replace a thread with a new one (that is: the old thread is order to stop using the StopFlag and only then the new thread is allowed to commence) Note: the new thread is created immediatly, it only waits for the old one to stop.
 void runDelayed(int milleseconds, java.lang.Runnable runnable)
          Execute something delayed
static void setStopFlag(boolean pStop)
          set the stop flag for the current thread
static void setStopFlag(java.lang.Runnable pRunnable, boolean pStop)
          set the stop flag for the specified Thread/Runnable
static void sleep(int milliseconds)
          Thread.sleep with an ignored InterruptedException
static void sleep(long milliseconds)
          Thread.sleep with an ignored InterruptedException
static void sleepForced(int milliseconds)
          Thread.sleep where an InterruptedException does not exit sleep
static void sleepForced(long sleeptime)
          Thread.sleep where an InterruptedException does not exit sleep
static void store(java.lang.Runnable runnable, java.lang.String key, java.lang.Object value)
          Store a specific value for this thread
static void store(java.lang.String key, java.lang.Object value)
           
static void store(java.lang.ThreadGroup runnable, java.lang.String key, java.lang.Object value)
          Store a specific value for this thread
static void waitForThreadToFinish(java.lang.Thread thread)
          Wait indefinitely for a thread to finish.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOURCECODE_VERSION

public static final java.lang.String SOURCECODE_VERSION
Standard variable for determining version of a class file.

See Also:
Constant Field Values
Constructor Detail

ThreadUtil

public ThreadUtil()
Method Detail

mayContinue

public static boolean mayContinue()
may the current thread continue


mustStop

public static boolean mustStop()
should the current thread stop


getStopFlag

public static boolean getStopFlag()
query the stop flag for the current thread


setStopFlag

public static void setStopFlag(boolean pStop)
set the stop flag for the current thread


getStopFlag

public static boolean getStopFlag(java.lang.Runnable pRunnable)
query the stop flag for the specified Thread/Runnable


setStopFlag

public static void setStopFlag(java.lang.Runnable pRunnable,
                               boolean pStop)
set the stop flag for the specified Thread/Runnable


store

public static void store(java.lang.Runnable runnable,
                         java.lang.String key,
                         java.lang.Object value)
Store a specific value for this thread


store

public static void store(java.lang.String key,
                         java.lang.Object value)

get

public static java.lang.Object get(java.lang.Runnable runnable,
                                   java.lang.String key)
fetch a specific value for this thread


get

public static java.lang.Object get(java.lang.String key)

remove

public static java.lang.Object remove(java.lang.Runnable runnable,
                                      java.lang.String key)
Remove a specific value for this thread


remove

public static java.lang.Object remove(java.lang.String key)

store

public static void store(java.lang.ThreadGroup runnable,
                         java.lang.String key,
                         java.lang.Object value)
Store a specific value for this thread


get

public static java.lang.Object get(java.lang.ThreadGroup runnable,
                                   java.lang.String key)
fetch a specific value for this thread


remove

public static void remove(java.lang.ThreadGroup runnable,
                          java.lang.String key)
Remove a specific value for this thread


sleep

public static void sleep(int milliseconds)
Thread.sleep with an ignored InterruptedException

Parameters:
milliseconds -

sleep

public static void sleep(long milliseconds)
Thread.sleep with an ignored InterruptedException

Parameters:
milliseconds -

sleepForced

public static void sleepForced(int milliseconds)
Thread.sleep where an InterruptedException does not exit sleep

Parameters:
milliseconds -

sleepForced

public static void sleepForced(long sleeptime)
Thread.sleep where an InterruptedException does not exit sleep

Parameters:
sleeptime - in milliseconds

waitForThreadToFinish

public static void waitForThreadToFinish(java.lang.Thread thread)
Wait indefinitely for a thread to finish. BEWARE: when a AWT thread calls this method, deadlock is almost certain. Use the SwingUtilities.waitForThreadToFinish when doing this in a GUI application!

Parameters:
thread -

getStackTrace

public static java.lang.String getStackTrace()
get the stack trace of the current thread as a String

Returns:

replaceThread

public static java.lang.Thread replaceThread(java.lang.Thread originalThread,
                                             java.lang.String name,
                                             java.lang.Runnable newRunnable)
Replace a thread with a new one (that is: the old thread is order to stop using the StopFlag and only then the new thread is allowed to commence) Note: the new thread is created immediatly, it only waits for the old one to stop. The spawing thread continues without waiting.


runDelayed

public void runDelayed(int milleseconds,
                       java.lang.Runnable runnable)
Execute something delayed

Parameters:
milleseconds -
runnable -

main

public static void main(java.lang.String[] args)


Copyright © 2010. All Rights Reserved.