java.lang.Thread
java.lang.Object
None
java.lang.Runnable
JDK 1.0 or later
The Thread class encapsulates all of the information about a single thread of control running in a Java environment. Thread objects are used to control threads in a multithreaded program.
The execution of Java code is always under the control of a Thread object. The Thread class provides a static method called currentThread() that can be used to get a reference to the Thread object that controls the current thread of execution.
In order for a Thread object to be useful, it must be associated with a method that it is supposed to run. Java provides two ways of associating a Thread object with a method:
After a thread is started, it dies when one of the following things happens:
public class java.lang.Thread extends java.lang.Object implements java.lang.Runnable { // Constants public final static int MAX_PRIORITY; public final static int MIN_PRIORITY; public final static int NORM_PRIORITY; // Constructors public Thread(); public Thread(Runnable target); public Thread(Runnable target, String name); public Thread(String name); public Thread(ThreadGroup group, Runnable target); public Thread(ThreadGroup group, Runnable target, String name); public Thread(ThreadGroup group, String name); // Class Methods public static int activeCount(); public static native Thread currentThread(); public static void dumpStack(); public static int enumerate(Thread tarray[]); public static boolean interrupted(); public static native void sleep(long millis); public static void sleep(long millis, int nanos); public static native void yield(); // Instance Methods public void checkAccess(); public native int countStackFrames(); public void destroy(); public final String getName(); public final int getPriority(); public final ThreadGroup getThreadGroup(); public void interrupt(); public final native boolean isAlive(); public final boolean isDaemon(); public boolean isInterrupted(); public final void join(); public final synchronized void join(long millis); public final synchronized void join(long millis, int nanos); public final void resume(); public void run(); public final void setDaemon(boolean on); public final void setName(String name); public final void setPriority(int newPriority); public synchronized native void start(); public final void stop(); public final synchronized void stop(Throwable o); public final void suspend(); public String toString(); }
The highest priority a thread can have.
The lowest priority a thread can have.
The default priority assigned to a thread.
If the checkAccess() method of the SecurityManager throws a SecurityException.
Creates a Thread object that belongs to the same ThreadGroup object as the current thread, has the same daemon attribute as the current thread, has the same priority as the current thread, and has a default name.
A Thread object created with this constructor invokes its own run() method when the Thread object's start() method is called. This is not useful unless the object belongs to a subclass of the Thread class that overrides the run() method.
Calling this constructor is equivalent to:
Thread(null, null, genName)
genName is an automatically generated name of the form "Thread-"+n, where n is an integer incremented each time a Thread object is created.
The name of this Thread object.
If the checkAccess() method of the SecurityManager throws a SecurityException.
Creates a Thread object that belongs to the same ThreadGroup object as the current thread, has the same daemon attribute as the current thread, has the same priority as the current thread, and has the specified name.
A Thread object created with this constructor invokes its own run() method when the Thread object's start() method is called. This is not useful unless the object belongs to a subclass of the Thread class that overrides the run() method.
Calling this constructor is equivalent to:
Thread(null, null, name)
The uniqueness of the specified Thread object's name is not checked, which may be a problem for programs that attempt to identify Thread objects by their name.
The ThreadGroup object that this Thread object is to be added to.
A reference to an object that implements the Runnable interface.
If the checkAccess() method of the SecurityManager throws a SecurityException.
Creates a Thread object that belongs to the specified ThreadGroup object, has the same daemon attribute as the current thread, has the same priority as the current thread, and has a default name.
A Thread object created with this constructor invokes the run() method of the specified Runnable object when the Thread object's start() method is called.
Calling this constructor is equivalent to:
Thread(group, target, genName)
genName is an automatically generated name of the form "Thread-"+n, where n is an integer that is incremented each time a Thread object is created.
The ThreadGroup object that this Thread object is to be added to.
A reference to an object that implements the Runnable interface.
The name of this Thread object.
If the checkAccess() method of the SecurityManager throws a SecurityException.
Creates a Thread object that belongs to the specified ThreadGroup object, has the same daemon attribute as the current thread, has the same priority as the current thread, and has the specified name.
A Thread object created with this constructor invokes the run() method of the specified Runnable object when the Thread object's start() method is called.
The uniqueness of the specified Thread object's name is not checked, which may be a problem for programs that attempt to identify Thread objects by their names.
The ThreadGroup object that this Thread object is to be added to.
The name of this Thread object.
If the checkAccess() method of the SecurityManager throws a SecurityException.
Creates a Thread object that belongs to the specified ThreadGroup object, has the same daemon attribute as the current thread, has the same priority as the current thread, and has the specified name.
A Thread object created with this constructor invokes its own run() method when the Thread object's start() method is called. This is not useful unless the object belongs to a subclass of the Thread class that overrides the run() method. Calling this constructor is equivalent to:
Thread(group, null, name)
The uniqueness of the specified Thread object's name is not checked, which may be a problem for programs that attempt to identify Thread objects by their name.
The current number of threads in the ThreadGroup of the currently running thread.
This method returns the number of threads in the ThreadGroup of the currently running thread for which the isAlive() method returns true.
A reference to the Thread object that controls the currently executing thread.
This method returns a reference to the Thread object that controls the currently executing thread.
This method outputs a stack trace of the currently running thread.
A reference to an array of Thread objects.
The number of Thread objects stored in the array.
This method stores a reference in the array for each of the Thread objects in the ThreadGroup of the currently running thread for which the isAlive() method returns true.
Calling this method is equivalent to:
currentThread().getThreadGroup().enumerate(tarray)
If the array is not big enough to contain references to all the Thread objects, only as many references as will fit are put into the array. No indication is given that some Thread objects were left out, so it is a good idea to call activeCount() before calling this method, to get an idea of how large to make the array.
true if the currently running thread has been interrupted; otherwise false.
This method determines whether or not the currently running thread has been interrupted.
The number of milliseconds that the currently running thread should sleep.
If another thread interrupts the currently running thread.
This method causes the currently running thread to sleep. The method does not return until at least the specified number of milliseconds have elapsed.
While a thread is sleeping, it retains ownership of all locks. The Object class defines a method called wait() that is similar to sleep() but causes the currently running thread to temporarily relinquish its locks.
The number of milliseconds that the currently running thread should sleep.
An additional number of nanoseconds to sleep.
If another thread interrupts the currently running thread.
This method causes the currently running thread to sleep. The method does not return until at least the specified number of milliseconds have elapsed.
While a thread is sleeping, it retains ownership of all locks. The Object class defines a method called wait() that is similar to sleep() but causes the currently running thread to temporarily relinquish its locks.
Note that Sun's reference implementation of Java does not attempt to implement the precision implied by this method. Instead, it rounds to the nearest millisecond (unless millis is 0, in which case it rounds up to 1 millisecond) and calls sleep(long).
This method causes the currently running thread to yield control of the processor so that another thread can be scheduled.
If the checkAccess() method of the SecurityManager throws a SecurityException.
This method determines if the currently running thread has permission to modify this Thread object.
The number of pending method invocations on this thread's stack.
This method returns the number of pending method invocations on this thread's stack.
This method is meant to terminate this thread without any of the usual cleanup (i.e., any locks held by the thread are not released). This method provides a last-resort way to terminate a thread. While a thread can defeat its stop() method by catching objects thrown from it, there is nothing that a thread can do to stop itself from being destroyed.
Note that Sun's reference implementation of Java does not implement the documented functionality of this method. Instead, the implementation of this method just throws a NoSuchMethodError.
The name of this thread.
This method returns the name of this Thread object.
The priority of this thread.
This method returns the priority of this Thread object.
The ThreadGroup of this thread.
This method returns a reference to the ThreadGroup that this Thread object belongs to.
This method interrupts this Thread object.
Note that prior to version 1.1, Sun's reference implementation of Java does not implement the documented functionality of this method. Instead, the method just sets a private flag that indicates that an interrupt has been requested. None of the methods that should throw an InterruptedException currently do. However, the interrupted() and isInterrupted() methods do return true after this method has been called.
true if this thread is alive; otherwise false.
This method determines whether or not this Thread object is alive. A Thread object is alive if it has been started and has not yet died. In other words, it has been scheduled to run before and can still be scheduled to run again. A thread is generally alive after its start() method is called and until its stop() method is called.
true if the thread is a daemon thread; otherwise false.
This method determines whether or not this thread is a daemon thread, based on the value of the daemon attribute of this Thread object.
true if this thread has been interrupted; otherwise false.
This method determines whether or not this Thread object has been interrupted.
If another thread interrupts this thread.
This method allows the thread that calls it to wait for the Thread associated with this method to die. The method returns when the Thread dies. If this thread is already dead, then this method returns immediately.
The maximum number of milliseconds to wait for this thread to die.
If another thread interrupts this thread.
This method causes a thread to wait to die. The method returns when this Thread object dies or after the specified number of milliseconds has elapsed, whichever comes first. However, if the specified number of milliseconds is zero, the method will wait forever for this thread to die. If this thread is already dead, the method returns immediately.
The maximum number of milliseconds to wait for this thread to die.
An additional number of nanoseconds to wait.
If another thread interrupts this thread.
This method causes a thread to wait to die. The method returns when this Thread object dies or after the specified amount of time has elapsed, whichever comes first. However, if millis and nanos are zero, the method will wait forever for this thread to die. If this thread is already dead, the method returns immediately.
Note that Sun's reference implementation of Java does not attempt to implement the precision implied by this method. Instead, it rounds to the nearest millisecond (unless millis is 0, in which case it rounds up to 1 millisecond) and calls join(long).
If the checkAccess() method of the SecurityManager throws a SecurityException.
This method resumes a suspended thread. The method causes this Thread object to once again be eligible to be run. Calling this method for a thread that is not suspended has no effect.
Runnable.run()
A Thread object's start() method causes the thread to invoke a run() method. If this Thread object was created without a specified Runnable object, the Thread object's own run() method is executed. This behavior is only useful in a subclass of Thread that overrides this run() method, since the run() method of the Thread class does not do anything.
The new value for this thread's daemon attribute.
If this method is called after this thread has been started and while it is still alive.
If the checkAccess() method of the SecurityManager throws a SecurityException.
This method sets the daemon attribute of this Thread object to the given value. This method must be called before the thread is started. If a thread dies and there are no other threads except daemon threads alive, the Java virtual machine stops.
The new name for this thread.
If the checkAccess() method of the SecurityManager throws a SecurityException.
This method sets the name of this Thread object to the given value. The uniqueness of the specified Thread object's name is not checked, which may be a problem for programs that attempt to identify Thread objects by their name.
The new priority for this thread.
If the given priority is less than MIN_PRIORITY or greater than MAX_PRIORITY.
If the checkAccess() method of the SecurityManager throws a SecurityException.
This method sets the priority of this Thread to the given value.
If this Thread object's start() method has been called before.
This method starts this Thread object, allowing it to be scheduled for execution. The top-level code that is executed by the thread is the run() method of the Runnable object specified in the constructor that was used to create this object. If no such object was specified, the top-level code executed by the thread is this object's run() method.
It is not permitted to start a thread more than once.
If the checkAccess() method of the SecurityManager throws a SecurityException.
This method causes this Thread object to stop executing by throwing a ThreadDeath object. The object is thrown in this thread, even if the method is called from a different thread. This thread is forced to stop whatever it is doing and throw a newly created ThreadDeath object. If this thread was suspended, it is resumed; if it was sleeping, it is awakened. Normally, you should not catch ThreadDeath objects in a try statement. If you need to catch ThreadDeath objects to detect a Thread is about to die, the try statement that catches ThreadDeath objects should rethrow them.
When an object is thrown out of the run() method associated with a Thread, the uncaughtException() method of the ThreadGroup for that Thread is called. The uncaughtException() method normally outputs a stack trace. However, uncaughtException() treats a ThreadDeath object as a special case by not outputting a stack trace. When the uncaughtException() method returns, the thread is dead. The thread is never scheduled to run again.
If this Thread object's stop() method is called before this thread is started, the ThreadDeath object is thrown as soon as the thread is started.
The object to be thrown.
If the checkAccess() method of the SecurityManager throws a SecurityException.
This method causes this Thread object to stop executing by throwing the given object. Normally, the stop() method that takes no arguments and throws a ThreadDeath object should be called instead of this method. However, if it is necessary to stop a thread by throwing some other type of object, this method can be used.
The object is thrown in this thread, even if the method is called from a different thread. This thread is forced to stop whatever it is doing and throw the Throwable object o. If this thread was suspended, it is resumed; if it was sleeping, it is awakened.
When an object is thrown out of the run() method associated with a Thread, the uncaughtException() method of the ThreadGroup for that Thread is called. If the thrown object is not an instance of the ThreadDeath class, uncaughtException() calls the thrown object's printStackTrace() method and then the thread dies. The thread is never scheduled to run again.
If this Thread object's stop() method is called before this thread is started, the ThreadDeath object is thrown as soon as the thread is started.
If the checkAccess() method of the SecurityManager throws a SecurityException.
This method suspends a thread. The method causes this Thread object to temporarily be ineligible to be run. The thread becomes eligible to be run again after its resume() method is called. Calling this method for a thread that is already suspended has no effect.
A string representation of this Thread object.
Object.toString()
This method returns a string representation of this Thread object.
Method |
Inherited From |
Method |
Inherited From |
---|---|---|---|
clone() |
Object |
equals(Object) |
Object |
finalize() |
Object |
getClass() |
Object |
hashCode() |
Object |
notify() |
Object |
notifyAll() |
Object |
wait() |
Object |
wait(long) |
Object |
wait(long, int) |
Object |
Exceptions; Object; Runnable; SecurityManager; ThreadGroup; Threads 8
This HTML Help has been published using the chm2web software. |