This class encapsulates all information
about a single thread of control running on the Java interpreter. To
create a thread, you must either pass a Runnable
object (i.e., an object that implements the
Runnable interface by defining a run(
) method) to the Thread constructor or
subclass THRead so that it defines its own
run( ) method. The run( )
method of the Thread or of the specified
Runnable object is the body of the thread. It
begins executing when the start( ) method of the
Thread object is called. The thread runs until the
run( ) method returns. isAlive(
) returns TRue if a thread has been
started, and the run( ) method has not yet exited.
The static methods of this class
operate on the currently running thread. currentThread(
) returns the Thread object of the
currently running code. sleep( ) makes the current
thread stop for a specified amount of time. yield(
) makes the current thread give up control to any other
threads of equal priority that are waiting to run.
holdsLock( ) tests whether the current thread
holds a lock (through a synchronized method or
statement) on the specified object; this Java 1.4 method is often
useful with an assert statement.
The instance methods may be called by
one thread to operate on a different thread. checkAccess(
) checks whether the running thread has permission to
modify a THRead object and throws a
SecurityException if it does not. join(
) waits for a thread to die. interrupt(
) wakes up a waiting or sleeping thread (with an
InterruptedException) or sets an interrupted flag
on a nonsleeping thread. A thread can test its own interrupted flag
with the static interrupted(
) method or can test the flag of another
thread with isInterrupted( ). Calling
interrupted( ) implicitly clears the interrupted
flag, but calling isInterrupted( ) does not.
Methods related to sleep( ) and
interrupt( ) are the wait( )
and notify( ) methods defined by the
Object class. Calling wait( )
causes the current thread to block until the
object's notify( ) method is
called by another thread.
setName( ) sets
the name of a thread, which is purely optional. setPriority(
) sets the priority of the thread. Higher priority threads
run before lower priority threads. Java does not specify what happens
to multiple threads of equal priority; some systems perform
time-slicing and share the CPU between such threads. On other
systems, one compute-bound thread that does not call yield(
) may starve another thread of the same priority.
setDaemon( ) sets a boolean flag that specifies
whether this thread is a daemon or not. The Java VM keeps running as
long as at least one nondaemon thread is running. Call
getThreadGroup( ) to obtain the
ThreadGroup of which a thread is part. In Java 1.2
and later, use setContextClassLoader( ) to specify
the ClassLoader to be used to load any classes
required by the thread.
suspend( ),
resume( ), and stop( ) suspend,
resume, and stop a given thread, respectively, but all three methods
are deprecated because they are inherently unsafe and can cause
deadlock. If a thread must be stoppable, have it periodically check a
flag and exit if the flag is set.
In Java 1.4 and later, the four-argument Thread( )
constructor allows you to specify the "stack
size" parameter for the thread. Typically, larger
stack sizes allow threads to recurse more deeply before running out
of stack space. Smaller stack sizes reduce the fixed per-thread
memory requirements and may allow more threads to exist concurrently.
The meaning of this argument is implementation dependent, and
implementations may even ignore it.
Java 5.0 adds important new features to
this class. getId( ) returns a unique
long
identifier for the thread. getState(
)
returns the state of the thread as an enumerated constant of type
Thread.State.
Thread.UncaughtExceptionHandler
defines an API for handling
exceptions that
cause the run( ) method of the thread to exit.
Register a handler of this type with
setUncaughtExceptionHandler(
) or register a default handler with the static
methods setDefaultUncaughtExceptionHandler(
). Obtain a snapshot of a
thread's current stack trace with
getStackTrace( )
. This returns an array of
StackTraceElement objects: the first element of
the array is the most recent method invocation and the last element
is the least recent. The static getAllStackTraces(
) returns stack traces for all running threads
(the traces may be obtained at different times for different
threads).

public class Thread implements Runnable {
// Public Constructors
public Thread( );
public Thread(String name);
public Thread(Runnable target);
public Thread(Runnable target, String name);
public Thread(ThreadGroup group, String name);
public Thread(ThreadGroup group, Runnable target);
public Thread(ThreadGroup group, Runnable target, String name);
1.4 public Thread(ThreadGroup group, Runnable target, String name, long stackSize);
// Public Constants
public static final int MAX_PRIORITY; =10
public static final int MIN_PRIORITY; =1
public static final int NORM_PRIORITY; =5
// Nested Types
5.0 public enum State;
5.0 public interface UncaughtExceptionHandler;
// Public Class Methods
public static int activeCount( );
public static Thread currentThread( ); native
public static void dumpStack( );
public static int enumerate(Thread[ ] tarray);
5.0 public static java.util.Map<Thread,StackTraceElement[ ]> getAllStackTraces( );
5.0 public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler( );
1.4 public static boolean holdsLock(Object obj); native
public static boolean interrupted( );
5.0 public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh);
public static void sleep(long millis) throws InterruptedException; native
public static void sleep(long millis, int nanos) throws InterruptedException;
public static void yield( ); native
// Public Instance Methods
public final void checkAccess( );
1.2 public ClassLoader getContextClassLoader( );
5.0 public long getId( ); default:7
public final String getName( ); default:"Thread-0"
public final int getPriority( ); default:5
5.0 public StackTraceElement[ ] getStackTrace( );
5.0 public Thread.State getState( );
public final ThreadGroup getThreadGroup( );
5.0 public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler( ); default:ThreadGroup
public void interrupt( );
public final boolean isAlive( ); native default:false
public final boolean isDaemon( ); default:false
public boolean isInterrupted( ); default:false
public final void join( ) throws InterruptedException;
public final void join(long millis) throws InterruptedException; synchronized
public final void join(long millis, int nanos) throws InterruptedException; synchronized
1.2 public void setContextClassLoader(ClassLoader cl);
public final void setDaemon(boolean on);
public final void setName(String name);
public final void setPriority(int newPriority);
5.0 public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh);
public void start( ); synchronized
// Methods Implementing Runnable
public void run( );
// Public Methods Overriding Object
public String toString( );
// Deprecated Public Methods
# public int countStackFrames( ); native
# public void destroy( );
# public final void resume( );
# public final void stop( );
# public final void stop(Throwable obj); synchronized
# public final void suspend( );
}