Packages This Package Prev Next Index
§1.19 Class Thread
public class java.lang.Thread
extends java.lang.Object (I-§1.12)
implements java.lang.Runnable (I-§1.23)
{
// Fields
public final static int MAX_PRIORITY; §1.19.1
public final static int MIN_PRIORITY; §1.19.2
public final static int NORM_PRIORITY; §1.19.3
// Constructors
public Thread(); §1.19.4
public Thread(Runnable target); §1.19.5
public Thread(Runnable target, String name); §1.19.6
public Thread(String name); §1.19.7
public Thread(ThreadGroup group, Runnable target); §1.19.8
public Thread(ThreadGroup group, §1.19.9
Runnable target, String name);
public Thread(ThreadGroup group, String name); §1.19.10
// Methods
public static int activeCount(); §1.19.11
public void checkAccess(); §1.19.12
public int countStackFrames(); §1.19.13
public static Thread currentThread(); §1.19.14
public void destroy(); §1.19.15
public static void dumpStack(); §1.19.16
public static int enumerate(Thread tarray[]); §1.19.17
public final String getName(); §1.19.18
public final int getPriority(); §1.19.19
public final ThreadGroup getThreadGroup(); §1.19.20
public void interrupt(); §1.19.21
public static boolean interrupted(); §1.19.22
public final boolean isAlive(); §1.19.23
public final boolean isDaemon(); §1.19.24
public boolean isInterrupted(); §1.19.25
public final void join(); §1.19.26
public final void join(long millis); §1.19.27
public final void join(long millis, int nanos) §1.19.28
public final void resume(); §1.19.29
public void run(); §1.19.30
public final void setDaemon(boolean on); §1.19.31
public final void setName(String name); §1.19.32
public final void setPriority(int newPriority); §1.19.33
public static void sleep(long millis); §1.19.34
public static void sleep(long millis, int nanos) §1.19.35
public void start(); §1.19.36
public final void stop(); §1.19.37
public final void stop(Throwable obj); §1.19.38
public final void suspend(); §1.19.39
public String toString(); §1.19.40
public static void yield(); §1.19.41
}
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:
The other way to create a thread is to is to declare a class which implements the Runnable
interface (I-§1.23). 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:
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.
MAX_PRIORITY
public final static int MAX_PRIORITY = 10
- The maximum priority that a thread can have.
MIN_PRIORITY
public final static int MIN_PRIORITY = 1
- The minimum priority that a thread can have.
NORM_PRIORITY
public final static int NORM_PRIORITY = 5
- The default priority that is assigned to a thread.
Thread
public Thread()
- Allocates a new Thread object. This constructor has the same effect as
Thread(null, null, gname) (I-§1.19.9) where gname is a newly generated name.
Automatically generated names are of the form "Thread-"+n where n is an
integer.
Thread
public Thread(Runnable target)
- Allocates a new Thread object. This constructor has the same effect as
Thread(null, target, gname) (I-§1.19.9) where gname is a newly generated
name. Automatically generated names are of the form "Thread-"+n where n
is an integer.
- Parameters:
target
-
the object whose run method is called
Thread
public Thread(Runnable target, String name)
- Allocates a new Thread object. This constructor has the same effect as
Thread(null, target, name) (I-§1.19.9).
- Parameters:
target
-
the object whose run method is called
name
-
the name of the new thread
Thread
public Thread(String name)
- Allocates a new Thread object. This constructor has the same effect as
Thread(null, null, name) (I-§1.19.9).
- Parameters:
name
-
the name of the new thread
Thread
public Thread(ThreadGroup group, Runnable target)
- Allocates a new Thread object. This constructor has the same effect as
Thread(group, target, gname) (I-§1.19.9) where gname is a newly generated
name. Automatically generated names are of the form "Thread-"+n where n
is an integer.
- Parameters:
group
-
the thread group
target
-
the object whose run method is called
- Throws
- SecurityException (I-§1.43)
- If the current thread cannot create a thread in the specified thread
group.
Thread
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 (I-§1.20.5) 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 (I-§1.23.1) is
called when this thread is started. If the target argument is null, this
thread's run method (I-§1.19.30) 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 (I-§1.19.33) 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 (I-§1.19.31) may be used to change whether or not a
thread is a dæmon.
- Parameters:
group
-
the thread group
target
-
the object whose run method is called
name
-
the name of the new thread
- Throws
- SecurityException (I-§1.43)
- If the current thread cannot create a thread in the specified thread
group
Thread
public Thread(ThreadGroup group, String name)
- Allocates a new Thread object. This constructor has the same effect as
Thread(group, null, name) (I-§1.19.9)
- Parameters:
group
-
the thread group
name
-
the name of the new thread
- Throws
- SecurityException (I-§1.43)
- If the current thread cannot create a thread in the specified thread
group.
activeCount
public static int activeCount()
- Returns:
- the current number of threads in this thread's thread group.
checkAccess
public void checkAccess()
- Determines if the currently running thread has permission to modify this
thread.
- If there is a security manager, its checkAccess method (I-§1.15.4) is called
with this thread as its argument. This may result in throwing a SecurityException.
- Throws
- SecurityException (I-§1.43)
- If the current thread is not allowed to access this thread.
countStackFrames
public int countStackFrames()
- Counts the number of stack frames in this thread. The thread must be suspended.
- Returns:
- the number of stack frames in this thread.
- Throws
- IllegalThreadStateException (I-§1.34)
- If this thread is not suspended.
currentThread
public static Thread currentThread()
- Find the currently executing thread.
- Returns:
- the currently executing thread.
destroy1
public void destroy()
- Destroys this thread, without any cleanup. Any monitors it has locked
remain locked.
dumpStack
public static void dumpStack()
- Prints a stack trace of the current thread. Used only for debugging.
- See Also:
- printStackTrace in class Throwable (I-§1.21.5).
enumerate
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 (I-§1.20.7) of this thread's
thread group with the array argument.
- Returns:
- the number of threads put into the array.
getName
public final String getName()
- Returns:
- this thread's name.
getPriority
public final int getPriority()
- Returns:
- this thread's current priority.
getThreadGroup
public final ThreadGroup getThreadGroup()
- Returns:
- this thread's thread group.
interrupt
public void interrupt()
- Interrupts this thread.
interrupted
public static boolean interrupted()
- Returns:
- true if the current thread has been interrupted; false otherwise.
isAlive
public final boolean isAlive()
- Determines if this thread is alive A thread is alive if it has been started and
has not yet died.
- Returns:
- true if this thread is alive; false otherwise.
isDaemon
public final boolean isDaemon()
- Returns:
- true if this thread is a dæmon thread; false otherwise.
isInterrupted
public boolean isInterrupted()
- Returns:
- true if this thread has been interrupted; false otherwise.
join
public final void join()
throws InterruptedException
- Waits for this thread to die.
- Throws
- InterruptedException (I-§1.37)
- Another thread has interrupted the current thread.
join
public final void join(long millis)
throws InterruptedException
- Waits at most millis milliseconds for for this thread to die. A timeout of 0
means to wait forever.
- Parameters:
millis
-
the time to wait in milliseconds
- Throws
- InterruptedException (I-§1.37)
- Another thread has interrupted the current thread.
join
public final void join(long millis, int nanos)
throws InterruptedException
- Waits at most millis milliseconds plus nanos nanoseconds for this thread to
die.
- Parameters:
millis
-
the time to wait in milliseconds
nanos
-
0-999999 additional nanoseconds to wait
- Throws
- InterruptedException (I-§1.37)
- Another thread has interrupted the current thread.
resume
public final void resume()
- Resumes a suspended thread.
- First, the checkAccess method (I-§1.19.12) of this thread is called with no
arguments. This may result in throwing a SecurityException (in the current
thread).
- If the thread is alive (I-§1.19.23) but suspended, it is resumed and is permitted to make progress in its execution.
- Throws
- SecurityException (I-§1.43)
- If the current thread cannot modify this thread.
run
public void run()
- If this thread was constructed using a separate Runnable run object (see
I-§1.19.9 on page 141 for more information), then that Runnable object's
run method (I-§1.23.1) is called. Otherwise, this method does nothing and
returns.
- Subclasses of Thread should override this method.
- See Also:
- start (I-§1.19.36)
stop (I-§1.19.37).
setDaemon
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.
- Parameters:
on
-
if true, marks this thread as a dæmon thread
- Throws
- IllegalThreadStateException (I-§1.34)
- If this thread is active.
setName
public final void setName(String name)
- Changes the name of this thread to be equal to the argument name.
- First the checkAccess method (I-§1.19.12) of this thread is called with no
arguments. This may result in throwing a SecurityException.
- Parameters:
name
-
the new name for this thread.
- Throws
- SecurityException (I-§1.43)
- If the current thread cannot modify this thread.
setPriority
public final void setPriority(int newPriority)
- Changes the priority of this thread
- First the checkAccess method (I-§1.19.12) 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 (I-§1.20.11) of the thread's
thread group (I-§1.19.20).
- Throws
- IllegalArgumentException (I-§1.32)
- If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.
- Throws
- SecurityException (I-§1.43)
- If the current thread cannot modify this thread.
sleep
public static void sleep(long millis)
throws InterruptedException
- Causes 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 I-§1.12.7 on page 72 for further discussion
of monitors).
- Parameters:
millis
-
the length of time to sleep in milliseconds
- Throws
- InterruptedException (I-§1.37)
- Another thread has interrupted this thread.
sleep
public static void sleep(long millis, int nanos)
throws InterruptedException
- Causes 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 I-§1.12.7
on page 72 for further discussion of monitors).
- Parameters:
millis
-
the length of time to sleep in milliseconds
nanos
-
0-999999 additional nanoseconds to sleep
- Throws
- InterruptedException (I-§1.37)
- Another thread has interrupted this thread.
start
public void start()
- Causes this thread to begin execution; the Java Virtual Machine calls the
run method (I-§1.19.30) 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).
- Throws
- IllegalThreadStateException (I-§1.34)
- If the thread was already started.
- See Also:
- run (I-§1.19.30)
stop (I-§1.19.37).
stop
public final void stop()
- Forces this thread to stop executing.
- First, the checkAccess method (I-§1.19.12) 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 (I-§1.59) 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
(I-§1.20.23) does not print out a message or otherwise notify the application if the uncaught exception is an instance of ThreadDeath.
- See Also:
- start (I-§1.19.36)
run (I-§1.19.30).
- Throws
- SecurityException (I-§1.43)
- If the current thread cannot modify this thread.
stop
public final void stop(Throwable obj)
- Forces the this thread to stop executing.
- First, the checkAccess method (I-§1.19.12) 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 (I-§1.19.37) 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.
- Parameters:
obj
-
the Throwable object to be thrown
- Throws
- SecurityException (I-§1.43)
- If the current thread cannot modify this thread.
suspend
public final void suspend()
- Suspends this thread.
- First, the checkAccess method (I-§1.19.12) of this thread is called with no
arguments. This may result in throwing a SecurityException (in the current
thread).
- If the thread is alive (I-§1.19.23), it is suspended and makes no further
progress unless and until it is resumed.
- Throws
- SecurityException (I-§1.43)
- If the current thread cannot modify this thread.
toString
public String toString()
- Returns:
- a string representation of this thread.
- Overrides:
- toString in class Object (I-§1.12.9).
yield
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.
Packages This Package Prev Next Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc.
All rights reserved
Please send any comments or corrections to doug.kramer@sun.com