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 application
has at least one thread running when it is started, the main thread, in the main
ThreadGroup
. The runtime keeps its own threads in the system thread
group.
There are two ways to execute code in a new thread.
You can either subclass
Thread
and overriding its
run()
method,
or construct a new
Thread
and pass a
Runnable
to the constructor.
In either case, the
start()
method must be called to actually execute
the new
Thread
.
Each
Thread
has an integer priority that affect how the thread is
scheduled by the OS. A new thread inherits the priority of its parent.
A thread's priority can be set using the
setPriority(int)
method.
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 the main thread. |
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 was deprecated
in API level 1.
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 was deprecated
in API level 1.
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).
|
||||||||||
|
Tests whether this is a daemon thread.
|
||||||||||
|
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 was deprecated
in API level 1.
Only useful in conjunction with deprecated method
suspend()
.
|
||||||||||
|
Calls the
run()
method of the Runnable object the receiver
holds.
|
||||||||||
|
Set the context ClassLoader for the receiver.
|
||||||||||
|
Marks this thread as a daemon thread.
|
||||||||||
|
Sets the default uncaught exception handler.
|
||||||||||
|
Sets the name of the Thread.
|
||||||||||
|
Sets the priority of this 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 was deprecated
in API level 1.
Stopping a thread in this manner is unsafe and can
leave your application and the VM in an unpredictable state.
|
||||||||||
|
This method was deprecated
in API level 1.
Stopping a thread in this manner is unsafe and can
leave your application and the VM in an unpredictable state.
|
||||||||||
|
This method was deprecated
in API level 1.
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.
This corresponds to (but does not have the same value as)
android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY
.
The minimum priority value allowed for a thread.
This corresponds to (but does not have the same value as)
android.os.Process.THREAD_PRIORITY_LOWEST
.
The normal (default) priority value assigned to the main thread.
This corresponds to (but does not have the same value as)
android.os.Process.THREAD_PRIORITY_DEFAULT
.
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 was deprecated
in API level 1.
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 was deprecated
in API level 1.
Not implemented.
Throws
UnsupportedOperationException
.
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 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 status
Returns
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 Thread
Tests whether this is a daemon thread. A daemon thread only runs as long as there are non-daemon threads running. When the last non-daemon thread ends, the runtime will exit. This is not normally relevant to applications with a UI.
Returns a
boolean
indicating whether the receiver has a
pending interrupt request (
true
) or not (
false
)
boolean
indicating the interrupt status
Blocks 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 was deprecated
in API level 1.
Only useful in conjunction with deprecated method
suspend()
.
Throws
UnsupportedOperationException
.
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 |
---|
Marks this thread as a daemon thread. A daemon thread only runs as long as there are non-daemon threads running. When the last non-daemon thread ends, the runtime will exit. This is not normally relevant to applications with a UI.
IllegalThreadStateException | - if this thread has already started. |
---|
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 this thread. If the requested priority is greater than the
parent thread group's
getMaxPriority()
, the group's maximum
priority will be used instead.
IllegalArgumentException |
- if the new priority is greater than
MAX_PRIORITY
or less than
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 this thread has already started. |
---|
This method was deprecated
in API level 1.
Stopping a thread in this manner is unsafe and can
leave your application and the VM in an unpredictable state.
Throws
UnsupportedOperationException
.
This method was deprecated
in API level 1.
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 was deprecated
in API level 1.
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.