java.lang
Class Thread

java.lang.Object
  extended by java.lang.Thread
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
Ballbot, Keyboard, LCPResponder, NXTRegulatedMotor.Controller, RConsole, RCXRotationSensor, RCXRotationSensor.Reader, Segoway, SimpleGPS

public class Thread
extends Object
implements Runnable

A thread of execution (or task). Now handles priorities, daemon threads and interruptions.


Nested Class Summary
static interface Thread.UncaughtExceptionHandler
           
 
Field Summary
static int MAX_PRIORITY
          The maximum priority that a thread can have.
static int MIN_PRIORITY
          The minimum priority that a thread can have.
static int NORM_PRIORITY
          The priority that is assigned to the primordial thread.
 
Constructor Summary
Thread()
           
Thread(Runnable target)
           
Thread(Runnable target, String name)
           
Thread(String name)
           
 
Method Summary
static Thread currentThread()
           
static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
          returns the current default exception handler if set, or null if none is set.
 String getName()
          Returns the string name of this thread.
 int getPriority()
           
 Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
          returns the current uncaught exception handler for this thread, if one has has been set or null if none is set.
 void interrupt()
          Set the interrupted flag.
static boolean interrupted()
          Tests the interrupted state of the current thread.
 boolean isAlive()
           
 boolean isDaemon()
          Set the daemon flag.
 boolean isInterrupted()
          Tests to see if the current thread has been interrupted but leaves the interrupted state unchanged.
 void join()
          Waits for this thread to die.
 void join(long timeout)
          Waits for up to timeout mS for this thread to die.
 void run()
          When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.
 void setDaemon(boolean on)
          Sets the state of the threads daemon flag.
static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
          Set the default exception handler.
 void setName(String name)
          Sets the string name associated with this thread.
 void setPriority(int priority)
          Set the priority of this thread.
 void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
          Sets the uncaught exception handler for this thread.
static void sleep(long aMilliseconds)
           
 void start()
           
static void yield()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_PRIORITY

public static final int MIN_PRIORITY
The minimum priority that a thread can have. The value is 1.

See Also:
Constant Field Values

NORM_PRIORITY

public static final int NORM_PRIORITY
The priority that is assigned to the primordial thread. The value is 5.

See Also:
Constant Field Values

MAX_PRIORITY

public static final int MAX_PRIORITY
The maximum priority that a thread can have. The value is 10.

See Also:
Constant Field Values
Constructor Detail

Thread

public Thread()

Thread

public Thread(String name)

Thread

public Thread(Runnable target)

Thread

public Thread(Runnable target,
              String name)
Method Detail

isAlive

public final boolean isAlive()

run

public void run()
Description copied from interface: Runnable
When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

Specified by:
run in interface Runnable

start

public final void start()

yield

public static void yield()

sleep

public static void sleep(long aMilliseconds)
                  throws InterruptedException
Throws:
InterruptedException

currentThread

public static Thread currentThread()

getPriority

public final int getPriority()

getName

public String getName()
Returns the string name of this thread.

Returns:
The name of this thread.

setName

public void setName(String name)
Sets the string name associated with this thread.

Parameters:
name - The new name of the thread.

setPriority

public final void setPriority(int priority)
Set the priority of this thread. Higher number have higher priority. The scheduler will always run the highest priority thread in preference to any others. If more than one thread of that priority exists the scheduler will time-slice them. In order for lower priority threas to run a higher priority thread must cease to be runnable. i.e. it must exit, sleep or wait on a monitor. It is not sufficient to just yield.

Threads inherit the priority of their parent. The primordial thread has priority NORM_PRIORITY.

Parameters:
priority - must be between MIN_PRIORITY and MAX_PRIORITY.

interrupt

public void interrupt()
Set the interrupted flag. If we are asleep we will wake up and an InterruptedException will be thrown.


interrupted

public static boolean interrupted()
Tests the interrupted state of the current thread. If it is interrupted it will true and clear the interrupted state. Otherwise it will return false.

Returns:
true if the current thread has been interrupted otherwise false

isInterrupted

public final boolean isInterrupted()
Tests to see if the current thread has been interrupted but leaves the interrupted state unchanged.

Returns:
true if the current thread has been interrupted otherwise false

isDaemon

public final boolean isDaemon()
Set the daemon flag. If a thread is a daemon thread its existence will not prevent a JVM from exiting.

Returns:
true if this thread has the daemon flag set otherwise false

setDaemon

public final void setDaemon(boolean on)
Sets the state of the threads daemon flag. If this flag is set then the system will not wait for it to exit when all other none daemon threads have exited.

Parameters:
on - the new state of the daemon flag

join

public final void join()
                throws InterruptedException
Waits for this thread to die.

Throws:
InterruptedException

join

public final void join(long timeout)
                throws InterruptedException
Waits for up to timeout mS for this thread to die.

Parameters:
timeout - The period in ms to wait for this thread to die
Throws:
InterruptedException

setDefaultUncaughtExceptionHandler

public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Set the default exception handler. This will be called for any uncaught exceptions thrown by threads which do not have an uncaught exception handler set.

Parameters:
handler - The new exception handler

getDefaultUncaughtExceptionHandler

public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
returns the current default exception handler if set, or null if none is set.

Returns:
the current exception handler

setUncaughtExceptionHandler

public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Sets the uncaught exception handler for this thread. This handler will be called for any uncaught exceptions thrown bu this thread.

Parameters:
handler - The new handler

getUncaughtExceptionHandler

public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
returns the current uncaught exception handler for this thread, if one has has been set or null if none is set.

Returns:
the current handler