Package java.lang |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
public class java.lang.Thread extends java.lang.Object implements java.lang.Runnable { // Fields 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); // Methods public static int activeCount(); public void checkAccess(); public int countStackFrames(); public static Thread currentThread(); public void destroy(); public static void dumpStack(); public static int enumerate(Thread tarray[]); public final String getName(); public final int getPriority(); public final ThreadGroup getThreadGroup(); public void interrupt(); public static boolean interrupted(); public final boolean isAlive(); public final boolean isDaemon(); public boolean isInterrupted(); public final void join(); public final void join(long millis); public final 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 static void sleep(long millis); public static void sleep(long millis, int nanos) public void start(); public final void stop(); public final void stop(Throwable obj); public final void suspend(); public String toString(); public static void yield(); }
A thread is a thread of executing in a program. The Java Virtual Machine allows an application to have multiple threads of executing running concurrently.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a dæmon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a dæmon thread if and only if the creating thread is a dæmon.
When a Java Virtual Machine starts up, there is usually a single non-dæmon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:
There are two ways to create a new thread of execution
One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread whose job is to compute primes larger than a stated value could be written as follows:
class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime ... } }
The following code would then create a thread and start it running:
PrimeThread p = new PrimeThread(143); p.start(); The other way to create a thread is to is to declare a class which implements the Runnable interface . That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. The same example in this other style looks like the following:
class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime ... } }
The following code would then create a thread and start it running:
PrimeRun p = new PrimeRun(143); new Thread(p).start(); Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.
public final static int MAX_PRIORITY = 10The maximum priority that a thread can have.
public final static int MIN_PRIORITY = 1The minimum priority that a thread can have.
public final static int NORM_PRIORITY = 5The default priority that is assigned to a thread.
public Thread()Allocates a new Thread object. This constructor has the same effect as Thread(null, null, gname) where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n where n is an integer.
public Thread(Runnable target)Allocates a new Thread object. This constructor has the same effect as Thread(null, target, gname) where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n where n is an integer.
Parameter Description target the object whose run method is called
public Thread(Runnable target, String name)Allocates a new Thread object. This constructor has the same effect as Thread(null, target, name) .
Parameter Description target the object whose run method is called name the name of the new thread
public Thread(String name)Allocates a new Thread object. This constructor has the same effect as Thread(null, null, name) .
Parameter Description name the name of the new thread
public Thread(ThreadGroup group, Runnable target)Allocates a new Thread object. This constructor has the same effect as Thread(group, target, gname) where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n where n is an integer.
Parameter Description group the thread group target the object whose run method is called Throw:
If the current thread cannot create a thread in the specified thread group.
public Thread(ThreadGroup group, Runnable target, String name)Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.
If group is not null, the checkAccess method of that thread group is called with no arguments; this may result in throwing a SecurityException; if group is null, the new process belongs to the same group as the thread this is created the new thread.
If the target argument is not null, the run method of the target is called when this thread is started. If the target argument is null, this thread's run method is called when this thread is started.
The priority of the newly created thread is set equal to the priority of the thread creating it, that is, the currently running thread. The method set-Priority may be used to change the priority to a new value.
The newly created thread is a initially marked as being a dæmon thread if and only if the thread creating it is currently marked as a dæmon thread. The method setDaemon may be used to change whether or not a thread is a dæmon.
Parameter Description group the thread group target the object whose run method is called name the name of the new thread Throw:
If the current thread cannot create a thread in the specified thread group
public Thread(ThreadGroup group, String name)Allocates a new Thread object. This constructor has the same effect as Thread(group, null, name)
Parameter Description group the thread group name the name of the new thread Throw:
If the current thread cannot create a thread in the specified thread group.
public static int activeCount()Return Value:
Returns the current number of threads in this thread's thread group.
public void checkAccess()Determines if the currently running thread has permission to modify this thread.
If there is a security manager, its checkAccess method is called with this thread as its argument. This may result in throwing a SecurityException.
Throw:
If the current thread is not allowed to access this thread.
public int countStackFrames()Counts the number of stack frames in this thread. The thread must be suspended.
Return Value:
Returns the number of stack frames in this thread.
Throw:
If this thread is not suspended.
public static Thread currentThread()Find the currently executing thread.
Return Value:
Returns the currently executing thread.
public void destroy()Destroys this thread, without any cleanup. Any monitors it has locked remain locked.
public static void dumpStack()Prints a stack trace of the current thread. Used only for debugging.
See Also: printStackTrace in class Throwable .
public static int enumerate(Thread tarray[])Copies into the array argument every thread in this thread's thread group. This method simple calls the enumerate method of this thread's thread group with the array argument.
Return Value:
Returns the number of threads put into the array.
public final String getName()Return Value:
Returns this thread's name.
public final int getPriority()Return Value:
Returns this thread's current priority.
public final ThreadGroup getThreadGroup()Return Value:
Returns this thread's thread group.
public void interrupt()Interrupts this thread.
public static boolean interrupted()Return Value:
Returns true if the current thread has been interrupted; false otherwise.
public final boolean isAlive()Determines if this thread is alive A thread is alive if it has been started and has not yet died.
Return Value:
Returns true if this thread is alive; false otherwise.
public final boolean isDaemon()Return Value:
Returns true if this thread is a dæmon thread; false otherwise.
public boolean isInterrupted()Return Value:
Returns true if this thread has been interrupted; false otherwise.
public final void join()throws InterruptedExceptionWaits for this thread to die.
Throw:
Another thread has interrupted the current thread.
public final void join(long millis)throws InterruptedExceptionWaits at most millis milliseconds for for this thread to die. A timeout of 0 means to wait forever.
Parameter Description millis the time to wait in milliseconds Throw:
Another thread has interrupted the current thread.
public final void join(long millis, int nanos)throws InterruptedExceptionWaits at most millis milliseconds plus nanos nanoseconds for this thread to die.
Parameter Description millis the time to wait in milliseconds nanos 0-999999 additional nanoseconds to wait Throw:
Another thread has interrupted the current thread.
public final void resume()Resumes a suspended thread.
First, the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException (in the current thread).
If the thread is alive but suspended, it is resumed and is permitted to make progress in its execution.
Throw:
If the current thread cannot modify this thread.
public void run()If this thread was constructed using a separate Runnable run object (see Thread for more information), then that Runnable object's run method is called. Otherwise, this method does nothing and returns.
Subclasses of Thread should override this method.
public final void setDaemon(boolean on)Marks this thread as either a dæmon thread or a user thread. The Java Virtual Machine exits when the only threads running are all dæmon threads.
This method must be called before the thread is started.
Parameter Description on if true, marks this thread as a dæmon thread Throw:
If this thread is active.
public final void setName(String name)Changes the name of this thread to be equal to the argument name.
First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException.
Parameter Description name the new name for this thread. Throw:
If the current thread cannot modify this thread.
public final void setPriority(int newPriority)Changes the priority of this thread
First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException.
Otherwise, the priority of this thread is set to the smaller of the specified newPriority and the maximum permitted priority of the thread's thread group .
Throw:
If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.
Throw:
If the current thread cannot modify this thread.
public static void sleep(long millis)throws InterruptedExceptionCauses the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors (see notify for further discussion of monitors).
Parameter Description millis the length of time to sleep in milliseconds Throw:
Another thread has interrupted this thread.
public static void sleep(long millis, int nanos)throws InterruptedExceptionCauses the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds. The thread does not lose ownership of any monitors (see notify for further discussion of monitors).
Parameter Description millis the length of time to sleep in milliseconds nanos 0-999999 additional nanoseconds to sleep Throw:
Another thread has interrupted this thread.
public void start()Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
Throw:
If the thread was already started.
public final void stop()Forces this thread to stop executing.
First, the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException (in the current thread).
The thread represented by the this thread is forced to stop whatever it is doing abnormally and to throw a newly created ThreadDeath object as an exception.
It is permitted to stop a thread that has not yet been started. If the thread is eventually started, it immediately terminates.
An application should not normally try to catch ThreadDeath unless it must do some extraordinary cleanup operation (note that the throwing of ThreadDeath will cause finally clauses of try statements to be executed before the thread officially dies). If a catch clause does catch a ThreadDeath object, it is important to rethrow the object so that the thread actually dies.
The top-level error handler that reacts to otherwise uncaught exceptions does not print out a message or otherwise notify the application if the uncaught exception is an instance of ThreadDeath.
Throw:
If the current thread cannot modify this thread.
public final void stop(Throwable obj)Forces the this thread to stop executing.
First, the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException (in the current thread).
If the argument obj is null, a NullPointerException is thrown (in the current thread).
The thread represented by this thread is forced to complete whatever it is doing abnormally and to throw the Throwable object obj as an exception. This is an unusual action to take; normally, the stop method that takes no arguments should be used.
It is permitted to stop a thread that has not yet been started. If the thread is eventually started, it immediately terminates.
Parameter Description obj the Throwable object to be thrown Throw:
If the current thread cannot modify this thread.
public final void suspend()Suspends this thread.
First, the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException (in the current thread).
If the thread is alive , it is suspended and makes no further progress unless and until it is resumed.
Throw:
If the current thread cannot modify this thread.
public String toString()Return Value:
Returns a string representation of this thread.
Overrides:
toString in class Object .
public static void yield()Causes the currently executing thread object to temporarily pause and allow other threads to execute.
(1)Unimplemented in Java 1.1.
© 1996 Sun Microsystems, Inc. All rights reserved.