com.marringtons.util
Class ThreadData

java.lang.Object
  extended byjava.lang.Thread
      extended bycom.marringtons.util.ThreadData
All Implemented Interfaces:
Runnable

public class ThreadData
extends Thread

This class adds thread specific data storage and retrieval - important for client/server systems to keep conversation specific information.

Author:
Paul Marrington

Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Method Summary
static Object get(Class itemClass)
          Retrieve data associated with this thread for single class instance per thread using the class name as a key.
static Object get(Object key)
          Retrieve data associated with this thread - if it has been set.
static Object get(Object key, Class itemClass)
          Retrieve application data associated with this thread, creating a new one if it does not exist.
static void reset()
          At the start of a converstion it is a good idea to clear the thread data as a different thread will act since last time and without this we can end up sharing data between users.
static void set(Object key, Object value)
          If you use the data() method that returns null rather than creating a new data item, you will need setData() to create the item.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, run, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

get

public static Object get(Object key,
                         Class itemClass)
Retrieve application data associated with this thread, creating a new one if it does not exist. Class must have a default contructor to be created when needed. Used to associate data with the running thread. If the thread will only have one instance of a class, consider using the overloaded data method that only takes a class name (@see #data(Class)).
 MyData myData = ThreadData.data( "myData", MyData.class);
 

Parameters:
key - Key used for retrieval.
itemClass - Class used to create item if it does not yet exist.
Returns:
Existing object or a newly created one if one does not exist yet.

reset

public static void reset()
At the start of a converstion it is a good idea to clear the thread data as a different thread will act since last time and without this we can end up sharing data between users.


get

public static Object get(Object key)
Retrieve data associated with this thread - if it has been set. It is used fairly rarely where the data() methods with creators are more popular. If the data to store requires parameters this method will be required.
 MyData myData = ThreadData.get( "myData");
 if (myData == null)
   ThreadData.set( "myData", myData = new MyData( "My Data"));
 

Parameters:
key - Key used for retrieval.
Returns:
Item or null if not found.

set

public static void set(Object key,
                       Object value)
If you use the data() method that returns null rather than creating a new data item, you will need setData() to create the item.
 MyData myData = ThreadData.data( "myData");
 if (myData == null)
   ThreadData.setData( "myData", myData = new MyData( "My Data");
 

Parameters:
key - Key to save/retrieve the data item.
value - Content to be set to the new data item.

get

public static Object get(Class itemClass)
Retrieve data associated with this thread for single class instance per thread using the class name as a key. Class must have a default contructor to be created when needed.
 MyData myData = ThreadData.get( MyData.class);
 

Parameters:
itemClass - Class used to create item if it does not yet exist.
Returns:
Item or null if not found


Copyright © 2005 Paul Marrington http://library.marringtons.com