java.lang.Thread
A Thread is a unit of concurrent execution. It has its own
call stack for methods being called and their parameters. Threads 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 Threads to cooperate.
Nested Classes
Known Direct Subclasses
HandlerThread |
Handy class for starting a new thread that has a looper. |
Summary
Constants
|
|
|
Value |
|
int |
MAX_PRIORITY |
The maximum priority value allowed for a thread. |
10 |
0x0000000a |
int |
MIN_PRIORITY |
The minimum priority value allowed for a thread. |
1 |
0x00000001 |
int |
NORM_PRIORITY |
The normal (default) priority value assigned to threads. |
5 |
0x00000005 |
Public Constructors
Public Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Constants
public
static
final
int
MAX_PRIORITY
The maximum priority value allowed for a thread.
Constant Value:
10
(0x0000000a)
public
static
final
int
MIN_PRIORITY
The minimum priority value allowed for a thread.
Constant Value:
1
(0x00000001)
public
static
final
int
NORM_PRIORITY
The normal (default) priority value assigned to threads.
Constant Value:
5
(0x00000005)
Public Constructors
public
Thread()
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.
public
Thread(Runnable runnable)
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.
Parameters
runnable
| a java.lang.Runnable whose method run will
be executed by the new Thread |
public
Thread(Runnable runnable, String threadName)
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.
Parameters
runnable
| a java.lang.Runnable whose method run will
be executed by the new Thread |
threadName
| Name for the Thread being created |
public
Thread(String threadName)
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.
Parameters
threadName
| 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.
Parameters
group
| ThreadGroup to which the new Thread will belong |
runnable
| a java.lang.Runnable whose method run will
be executed by the new Thread |
Constructs a new Thread with a runnable object, the given name and
belonging to the ThreadGroup passed as parameter.
Parameters
group
| ThreadGroup to which the new Thread will belong |
runnable
| a java.lang.Runnable whose method run will
be executed by the new Thread |
threadName
| Name for the Thread being created |
Constructs a new Thread with no runnable object, the given name and
belonging to the ThreadGroup passed as parameter.
Parameters
group
| ThreadGroup to which the new Thread will belong |
threadName
| Name for the Thread being created |
public
Thread(ThreadGroup group, Runnable runnable, String threadName, long stackSize)
Constructs a new Thread with a runnable object, the given name and
belonging to the ThreadGroup passed as parameter.
Parameters
group
| ThreadGroup to which the new Thread will belong |
runnable
| a java.lang.Runnable whose method run will
be executed by the new Thread |
threadName
| Name for the Thread being created |
stackSize
| Platform dependent stack size |
Public Methods
public
static
int
activeCount()
Returns the number of active threads in the running thread's ThreadGroup
and its subgroups.
public
final
void
checkAccess()
This method 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.
public
int
countStackFrames()
This method is deprecated.
The results of this call were never well defined. To make
things worse, it would depend if the Thread was suspended or
not, and suspend was deprecated too.
Returns the number of stack frames in this thread.
public
static
Thread
currentThread()
Returns the instance of Thread that corresponds to the running Thread
which calls this method.
Returns
- a java.lang.Thread corresponding to the code that called
currentThread()
public
void
destroy()
This method is deprecated.
Not implemented.
Destroys the receiver without any monitor cleanup.
public
static
void
dumpStack()
Prints a text representation of the stack for this Thread.
public
static
int
enumerate(Thread[] threads)
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.
Parameters
threads
| array into which the Threads will be copied |
Returns
- How many Threads were copied over
Returns the stack traces of all the currently live threads and puts them into
the given map.
Returns
- A Map of current Threads to StackTraceElement arrays.
public
ClassLoader
getContextClassLoader()
Returns the context ClassLoader for the receiver.
If the conditions
- there is a security manager
- the caller's class loader is not null
- the caller's class loader is not the same as or an
ancestor of contextClassLoader
are satisfied, a security check for
RuntimePermission("getClassLoader")
is performed first.
Returns
- ClassLoader The context ClassLoader
Returns the default exception handler that's executed when uncaught
exception terminates a thread.
public
long
getId()
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 life of the thread; the ID may be reused after the thread has
been terminated.
public
final
String
getName()
Returns the name of the receiver.
Returns
- the receiver's name (a java.lang.String)
public
final
int
getPriority()
Returns the priority of the receiver.
Returns
- the receiver's priority (an
int
)
Returns the current stack trace of the thread.
The RuntimePermission("getStackTrace")
is checked before
returning a result.
Returns
- An array of StackTraceElements.
Returns the current state of the thread for monitoring purposes.
public
final
ThreadGroup
getThreadGroup()
Returns the ThreadGroup to which the receiver belongs
Returns
- the receiver's ThreadGroup
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.
Returns
- An UncaughtExceptionHandler instance or
null
.
public
static
boolean
holdsLock(Object object)
Returns whether the current thread has a monitor lock on the specified
object.
Parameters
object
| the object to test for the monitor lock |
Returns
- true when the current thread has a monitor lock on the specified
object
public
void
interrupt()
Posts an interrupt request to the receiver.
public
static
boolean
interrupted()
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.
public
final
boolean
isAlive()
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.
public
final
boolean
isDaemon()
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.
When the last non-daemon Thread ends, the whole program ends no matter if
it had daemon Threads still running or not.
public
boolean
isInterrupted()
Returns a
boolean
indicating whether the receiver has a
pending interrupt request (
true
) or not (
false
)
public
final
void
join()
Blocks the current Thread (
Thread.currentThread()
) until
the receiver finishes its execution and dies.
public
final
void
join(long millis, int nanos)
Blocks the current Thread (
Thread.currentThread()
) until
the receiver finishes its execution and dies or the specified timeout
expires, whatever happens first.
Parameters
millis
| The maximum time to wait (in milliseconds). |
nanos
| Extra nanosecond precision |
public
final
void
join(long millis)
Blocks the current Thread (
Thread.currentThread()
) until
the receiver finishes its execution and dies or the specified timeout
expires, whatever happens first.
Parameters
millis
| The maximum time to wait (in milliseconds). |
public
final
void
resume()
This method is deprecated.
Used with deprecated method suspend()
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.
public
void
run()
Calls the
run()
method of the Runnable object the receiver
holds. If no Runnable is set, does nothing.
public
void
setContextClassLoader(ClassLoader cl)
Set the context ClassLoader for the receiver.
The RuntimePermission("setContextClassLoader")
is checked prior to setting the handler.
Parameters
cl
| The context ClassLoader |
public
final
void
setDaemon(boolean isDaemon)
Set if the receiver is a daemon Thread or not. This can only be done
before the Thread starts running.
Parameters
isDaemon
| A boolean indicating if the Thread should be daemon or
not |
public
static
void
setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Sets the default uncaught exception handler.
The RuntimePermission("setDefaultUncaughtExceptionHandler")
is checked prior to setting the handler.
Parameters
handler
| The handler to set or null . |
public
final
void
setName(String threadName)
Sets the name of the receiver.
Parameters
threadName
| new name for the Thread |
public
final
void
setPriority(int priority)
Sets the priority of the receiver. 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().
Parameters
priority
| new priority for the Thread |
Sets the default uncaught exception handler.
Parameters
handler
| The handler to set or null . |
public
static
void
sleep(long time, int nanos)
Causes the thread which sent this message to sleep an interval of time
(given in milliseconds). The precision is not guaranteed - the Thread may
sleep more or less than requested.
Parameters
time
| The time to sleep in milliseconds. |
nanos
| Extra nanosecond precision |
public
static
void
sleep(long time)
Causes the thread which sent this message to sleep an interval of time
(given in milliseconds). The precision is not guaranteed - the Thread may
sleep more or less than requested.
Parameters
time
| The time to sleep in milliseconds. |
public
synchronized
void
start()
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()
).
public
final
void
stop()
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.
public
final
synchronized
void
stop(Throwable throwable)
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()
.
Parameters
throwable
| Throwable object to be thrown by the Thread |
public
final
void
suspend()
This method is deprecated.
May cause deadlocks.
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.
public
String
toString()
Returns a string containing a concise, human-readable description of the
receiver.
Returns
- a printable representation for the receiver.
public
static
void
yield()
Causes the thread which sent this message to yield execution to another
Thread that is ready to run. The actual scheduling is
implementation-dependent.