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. | |||||||||||
Is used for operations that require approval from a SecurityManager.
| |||||||||||
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 the stack traces of all the currently live threads and puts them into the given map. | |||||||||||
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 the a stack trace representing the current execution state of
this Thread.
| |||||||||||
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 |
SecurityException | if group.checkAccess() fails with a
SecurityException |
---|---|
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 |
SecurityException | if group.checkAccess() fails with a
SecurityException |
---|---|
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 |
SecurityException | if group.checkAccess() fails with a
SecurityException |
---|---|
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. |
SecurityException | if group.checkAccess() fails with a
SecurityException |
---|---|
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
Is used for operations that require approval from a SecurityManager. If
there's none installed, this method is a no-op. If there's a
SecurityManager installed, checkAccess(Thread)
is
called for that SecurityManager.
SecurityException | if a SecurityManager is installed and it does not allow access to the Thread. |
---|
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 |
---|
SecurityException | if the installed SecurityManager fails
checkAccess(Thread) |
---|
Returns the stack traces of all the currently live threads and puts them into the given map.
SecurityException | if the current SecurityManager fails the
checkPermission(java.security.Permission)
call.
|
---|
Returns the context ClassLoader for this Thread.
If the conditions
RuntimePermission("getClassLoader")
is performed first.SecurityException | if the aforementioned security check fails. |
---|
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 the a stack trace representing the current execution state of this Thread.
The RuntimePermission("getStackTrace")
is checked before
returning a result.
SecurityException | if the current SecurityManager fails the
checkPermission(java.security.Permission)
call.
|
---|
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
. Unless the caller is
the currentThread()
, the method checkAccess()
is called
for the installed SecurityManager
, if any. This may result in a
SecurityException
being thrown. The further 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.
SecurityException | if checkAccess() fails with a SecurityException |
---|
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 |
---|
This method is deprecated.
Used with deprecated method suspend()
Resumes a suspended Thread. This is a no-op if the receiver was never suspended, or suspended and already resumed. If the receiver is suspended, however, makes it resume to the point where it was when it was suspended.
SecurityException | if checkAccess() fails with a SecurityException |
---|
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.
The RuntimePermission("setContextClassLoader")
is checked prior to setting the handler.
cl | The context ClassLoader |
---|
SecurityException | if the current SecurityManager fails the checkPermission call. |
---|
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 |
---|
SecurityException | if checkAccess() fails with a SecurityException |
---|
Sets the default uncaught exception handler. This handler is invoked in case any Thread dies due to an unhandled exception.
The RuntimePermission("setDefaultUncaughtExceptionHandler")
is checked prior to setting the handler.
handler | The handler to set or null . |
---|
SecurityException | if the current SecurityManager fails the checkPermission call. |
---|
Sets the name of the Thread.
threadName | the new name for the Thread |
---|
SecurityException | if checkAccess() fails with a
SecurityException |
---|
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 |
---|
SecurityException | if checkAccess() fails with a SecurityException |
---|---|
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 . |
---|
SecurityException | if the current SecurityManager fails the checkAccess call. |
---|
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.
Requests the receiver Thread to stop and throw the
throwable()
. The Thread is resumed if it was suspended
and awakened if it was sleeping, so that it can proceed to throw the
throwable()
.
throwable | Throwable object to be thrown by the Thread |
---|
SecurityException | if checkAccess() fails with a
SecurityException |
---|---|
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.
SecurityException | if checkAccess() fails with a
SecurityException |
---|
This method is deprecated.
May cause deadlocks.
Suspends this Thread. This is a no-op if the receiver is suspended. If
the receiver isAlive()
however, suspended it until
resume()
is sent to it. Suspend requests are not queued, which
means that N requests are equivalent to just one - only one resume
request is needed in this case.
SecurityException | if checkAccess() fails with a SecurityException |
---|
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.