java.lang.Object | |
↳ | java.lang.Thread |
Known Direct Subclasses |
A Thread
is a concurrent unit of execution. It has its own call stack
for methods being invoked, their arguments and local variables. Each virtual
machine instance has at least one main Thread
running when it is
started; typically, there are several others for housekeeping. The
application might decide to launch additional Thread
s for specific
purposes.
Thread
s in the same VM interact and synchronize by the use of shared
objects and monitors associated with these objects. Synchronized methods and
part of the API in Object
also allow Thread
s to cooperate.
There are basically two main ways of having a Thread
execute
application code. One is providing a new class that extends Thread
and overriding its run()
method. The other is providing a new
Thread
instance with a Runnable
object during its creation.
In both cases, the start()
method must be called to actually execute
the new Thread
.
Each Thread
has an integer priority that basically determines the
amount of CPU time the Thread
gets. It can be set using the
setPriority(int)
method. A Thread
can also be made a daemon,
which makes it run in the background. The latter also affects VM termination
behavior: the VM does not terminate automatically as long as there are
non-daemon threads running.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Thread.State | A representation of a thread's state. | ||||||||||
Thread.UncaughtExceptionHandler | Implemented by objects that want to handle cases where a thread is being terminated by an uncaught exception. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | MAX_PRIORITY | The maximum priority value allowed for a thread. | |||||||||
int | MIN_PRIORITY | The minimum priority value allowed for a thread. | |||||||||
int | NORM_PRIORITY | The normal (default) priority value assigned to threads. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new
Thread with no Runnable object and a
newly generated name. | |||||||||||
Constructs a new
Thread with a Runnable object and a
newly generated name. | |||||||||||
Constructs a new
Thread with a Runnable object and name
provided. | |||||||||||
Constructs a new
Thread with no Runnable object and the
name provided. | |||||||||||
Constructs a new
Thread with a Runnable object and a
newly generated name. | |||||||||||
Constructs a new
Thread with a Runnable object, the given
name and belonging to the ThreadGroup passed as parameter. | |||||||||||
Constructs a new
Thread with no Runnable object, the
given name and belonging to the ThreadGroup passed as parameter. | |||||||||||
Constructs a new
Thread with a Runnable object, the given
name and belonging to the ThreadGroup passed as parameter. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns the number of active
Thread s in the running Thread 's group and its subgroups. | |||||||||||
Does nothing.
| |||||||||||
This method is deprecated.
The results of this call were never well defined. To make
things worse, it would depend on whether the Thread was
suspended or not, and suspend was deprecated too.
| |||||||||||
Returns the Thread of the caller, that is, the current Thread.
| |||||||||||
This method is deprecated.
Not implemented.
| |||||||||||
Prints to the standard error stream a text representation of the current
stack for this Thread.
| |||||||||||
Copies an array with all Threads which are in the same ThreadGroup as the
receiver - and subgroups - into the array
threads passed as
parameter. | |||||||||||
Returns a map of all the currently live threads to their stack traces.
| |||||||||||
Returns the context ClassLoader for this Thread.
| |||||||||||
Returns the default exception handler that's executed when uncaught
exception terminates a thread.
| |||||||||||
Returns the thread's identifier.
| |||||||||||
Returns the name of the Thread.
| |||||||||||
Returns the priority of the Thread.
| |||||||||||
Returns an array of
StackTraceElement representing the current thread's stack. | |||||||||||
Returns the current state of the Thread.
| |||||||||||
Returns the ThreadGroup to which this Thread belongs.
| |||||||||||
Returns the thread's uncaught exception handler.
| |||||||||||
Indicates whether the current Thread has a monitor lock on the specified
object.
| |||||||||||
Posts an interrupt request to this
Thread . | |||||||||||
Returns a
boolean indicating whether the current Thread (
currentThread() ) has a pending interrupt request (
true ) or not (false ). | |||||||||||
Returns
true if the receiver has already been started and
still runs code (hasn't died yet). | |||||||||||
Returns a
boolean indicating whether the receiver is a
daemon Thread (true ) or not (false ) A
daemon Thread only runs as long as there are non-daemon Threads running. | |||||||||||
Returns a
boolean indicating whether the receiver has a
pending interrupt request (true ) or not (
false ) | |||||||||||
Blocks the current Thread (
Thread.currentThread() ) until
the receiver finishes its execution and dies. | |||||||||||
Blocks the current Thread (
Thread.currentThread() ) until
the receiver finishes its execution and dies or the specified timeout
expires, whatever happens first. | |||||||||||
Blocks the current Thread (
Thread.currentThread() ) until
the receiver finishes its execution and dies or the specified timeout
expires, whatever happens first. | |||||||||||
This method is deprecated.
Used with deprecated method
suspend()
| |||||||||||
Calls the
run() method of the Runnable object the receiver
holds. | |||||||||||
Set the context ClassLoader for the receiver.
| |||||||||||
Set if the receiver is a daemon Thread or not.
| |||||||||||
Sets the default uncaught exception handler.
| |||||||||||
Sets the name of the Thread.
| |||||||||||
Sets the priority of the Thread.
| |||||||||||
Sets the uncaught exception handler. | |||||||||||
Causes the thread which sent this message to sleep for the given interval
of time (given in milliseconds and nanoseconds).
| |||||||||||
Causes the thread which sent this message to sleep for the given interval
of time (given in milliseconds).
| |||||||||||
Starts the new Thread of execution.
| |||||||||||
This method is deprecated.
because stopping a thread in this manner is unsafe and can
leave your application and the VM in an unpredictable state.
| |||||||||||
This method is deprecated.
because stopping a thread in this manner is unsafe and can
leave your application and the VM in an unpredictable state.
| |||||||||||
This method is deprecated.
May cause deadlocks.
| |||||||||||
Returns a string containing a concise, human-readable description of the
Thread.
| |||||||||||
Causes the calling Thread to yield execution time to another Thread that
is ready to run.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.lang.Runnable
|
The maximum priority value allowed for a thread.
The minimum priority value allowed for a thread.
The normal (default) priority value assigned to threads.
Constructs a new Thread
with no Runnable
object and a
newly generated name. The new Thread
will belong to the same
ThreadGroup
as the Thread
calling this constructor.
Constructs a new Thread
with a Runnable
object and a
newly generated name. The new Thread
will belong to the same
ThreadGroup
as the Thread
calling this constructor.
runnable | a Runnable whose method run will be
executed by the new Thread |
---|
Constructs a new Thread
with a Runnable
object and name
provided. The new Thread
will belong to the same ThreadGroup
as the Thread
calling this constructor.
runnable | a Runnable whose method run will be
executed by the new Thread |
---|---|
threadName | the name for the Thread being created |
Constructs a new Thread
with no Runnable
object and the
name provided. The new Thread
will belong to the same ThreadGroup
as the Thread
calling this constructor.
threadName | the name for the Thread being created |
---|
Constructs a new Thread
with a Runnable
object and a
newly generated name. The new Thread
will belong to the ThreadGroup
passed as parameter.
group | ThreadGroup to which the new Thread will
belong |
---|---|
runnable | a Runnable whose method run will be
executed by the new Thread |
IllegalThreadStateException | if group.destroy() has already been done |
---|
Constructs a new Thread
with a Runnable
object, the given
name and belonging to the ThreadGroup
passed as parameter.
group | ThreadGroup to which the new Thread will belong |
---|---|
runnable | a Runnable whose method run will be
executed by the new Thread |
threadName | the name for the Thread being created |
IllegalThreadStateException | if group.destroy() has already been done |
---|
Constructs a new Thread
with no Runnable
object, the
given name and belonging to the ThreadGroup
passed as parameter.
group | ThreadGroup to which the new Thread will belong |
---|---|
threadName | the name for the Thread being created |
IllegalThreadStateException | if group.destroy() has already been done |
---|
Constructs a new Thread
with a Runnable
object, the given
name and belonging to the ThreadGroup
passed as parameter.
group | ThreadGroup to which the new Thread will
belong |
---|---|
runnable | a Runnable whose method run will be
executed by the new Thread |
threadName | the name for the Thread being created |
stackSize | a stack size for the new Thread . This has a highly
platform-dependent interpretation. It may even be ignored
completely. |
IllegalThreadStateException | if group.destroy() has already been done |
---|
Returns the number of active Thread
s in the running Thread
's group and its subgroups.
Thread
s
This method is deprecated.
The results of this call were never well defined. To make
things worse, it would depend on whether the Thread was
suspended or not, and suspend was deprecated too.
Returns the number of stack frames in this thread.
Returns the Thread of the caller, that is, the current Thread.
This method is deprecated.
Not implemented.
Destroys the receiver without any monitor cleanup.
Prints to the standard error stream a text representation of the current stack for this Thread.
Copies an array with all Threads which are in the same ThreadGroup as the
receiver - and subgroups - into the array threads
passed as
parameter. If the array passed as parameter is too small no exception is
thrown - the extra elements are simply not copied.
threads | array into which the Threads will be copied |
---|
Returns a map of all the currently live threads to their stack traces.
Returns the context ClassLoader for this Thread.
Returns the default exception handler that's executed when uncaught exception terminates a thread.
Thread.UncaughtExceptionHandler
or null
if
none exists.
Returns the thread's identifier. The ID is a positive long
generated on thread creation, is unique to the thread, and doesn't change
during the lifetime of the thread; the ID may be reused after the thread
has been terminated.
Returns the name of the Thread.
Returns an array of StackTraceElement
representing the current thread's stack.
Returns the current state of the Thread. This method is useful for monitoring purposes.
Thread.State
value.
Returns the ThreadGroup to which this Thread belongs.
Returns the thread's uncaught exception handler. If not explicitly set,
then the ThreadGroup's handler is returned. If the thread is terminated,
then null
is returned.
Thread.UncaughtExceptionHandler
instance or null
.
Indicates whether the current Thread has a monitor lock on the specified object.
object | the object to test for the monitor lock |
---|
Posts an interrupt request to this Thread
. The behavior depends on
the state of this Thread
:
Thread
s blocked in one of Object
's wait()
methods
or one of Thread
's join()
or sleep()
methods will
be woken up, their interrupt status will be cleared, and they receive an
InterruptedException
.
Thread
s blocked in an I/O operation of an
InterruptibleChannel
will have their interrupt
status set and receive an
ClosedByInterruptException
. Also, the channel
will be closed.
Thread
s blocked in a Selector
will have
their interrupt status set and return immediately. They don't receive an
exception in this case.
Returns a boolean
indicating whether the current Thread (
currentThread()
) has a pending interrupt request (
true
) or not (false
). It also has the side-effect of
clearing the flag.
boolean
indicating the interrupt statusReturns true
if the receiver has already been started and
still runs code (hasn't died yet). Returns false
either if
the receiver hasn't been started yet or if it has already started and run
to completion and died.
boolean
indicating the liveness of the ThreadReturns a boolean
indicating whether the receiver is a
daemon Thread (true
) or not (false
) A
daemon Thread only runs as long as there are non-daemon Threads running.
When the last non-daemon Thread ends, the whole program ends no matter if
it had daemon Threads still running or not.
boolean
indicating whether the Thread is a daemonReturns a boolean
indicating whether the receiver has a
pending interrupt request (true
) or not (
false
)
boolean
indicating the interrupt statusBlocks the current Thread (Thread.currentThread()
) until
the receiver finishes its execution and dies.
InterruptedException | if interrupt() was called for
the receiver while it was in the join() call |
---|
Blocks the current Thread (Thread.currentThread()
) until
the receiver finishes its execution and dies or the specified timeout
expires, whatever happens first.
millis | The maximum time to wait (in milliseconds). |
---|---|
nanos | Extra nanosecond precision |
InterruptedException | if interrupt() was called for
the receiver while it was in the join() call |
---|
Blocks the current Thread (Thread.currentThread()
) until
the receiver finishes its execution and dies or the specified timeout
expires, whatever happens first.
millis | The maximum time to wait (in milliseconds). |
---|
InterruptedException | if interrupt() was called for
the receiver while it was in the join() call |
---|
Calls the run()
method of the Runnable object the receiver
holds. If no Runnable is set, does nothing.
Set the context ClassLoader for the receiver.
cl | The context ClassLoader |
---|
Set if the receiver is a daemon Thread or not. This can only be done before the Thread starts running.
isDaemon | indicates whether the Thread should be daemon or not |
---|
Sets the default uncaught exception handler. This handler is invoked in case any Thread dies due to an unhandled exception.
handler | The handler to set or null. |
---|
Sets the priority of the Thread. Note that the final priority set may not be the parameter that was passed - it will depend on the receiver's ThreadGroup. The priority cannot be set to be higher than the receiver's ThreadGroup's maxPriority().
priority | new priority for the Thread |
---|
IllegalArgumentException | if the new priority is greater than Thread.MAX_PRIORITY or less than Thread.MIN_PRIORITY |
---|
Sets the uncaught exception handler. This handler is invoked in case this Thread dies due to an unhandled exception.
handler | The handler to set or null .
|
---|
Causes the thread which sent this message to sleep for the given interval of time (given in milliseconds and nanoseconds). The precision is not guaranteed - the Thread may sleep more or less than requested.
millis | The time to sleep in milliseconds. |
---|---|
nanos | Extra nanosecond precision |
InterruptedException | if interrupt() was called for this Thread while
it was sleeping |
---|
Causes the thread which sent this message to sleep for the given interval of time (given in milliseconds). The precision is not guaranteed - the Thread may sleep more or less than requested.
time | The time to sleep in milliseconds. |
---|
InterruptedException | if interrupt() was called for this Thread while
it was sleeping |
---|
Starts the new Thread of execution. The run()
method of
the receiver will be called by the receiver Thread itself (and not the
Thread calling start()
).
IllegalThreadStateException | if the Thread has been started before |
---|
This method is deprecated.
because stopping a thread in this manner is unsafe and can
leave your application and the VM in an unpredictable state.
Throws UnsupportedOperationException
.
NullPointerException | if throwable() is
null |
---|
This method is deprecated.
because stopping a thread in this manner is unsafe and can
leave your application and the VM in an unpredictable state.
Requests the receiver Thread to stop and throw ThreadDeath. The Thread is resumed if it was suspended and awakened if it was sleeping, so that it can proceed to throw ThreadDeath.
This method is deprecated.
May cause deadlocks.
Throws UnsupportedOperationException
.
Returns a string containing a concise, human-readable description of the Thread. It includes the Thread's name, priority, and group name.
Causes the calling Thread to yield execution time to another Thread that is ready to run. The actual scheduling is implementation-dependent.