Java Reference
In-Depth Information
14.14. Debugging Threads
A few
THRead
methods are designed to help you debug a multithreaded
application. These print-style debugging aids can be used to print the
state of an application. You can invoke the following methods on a
THRead
object to help you debug your threads:
public String
toString()
Returns a string representation of the thread, including its
name, its priority, and the name of its thread group.
public long
getId()
Returns a positive value that uniquely identifies this thread
while it is alive.
public Thread.State
getState()
Returns the current state of this thread.
Thread.State
is a nes-
ted enum that defines the constants:
NEW
,
RUNNABLE
,
BLOCKED
,
WAITING
,
TIMED_WAITING
, and
TERMINATED
. A newly created thread
has state
NEW
, until it is started, at which time it becomes
RUNNABLE
until it terminates and becomes
TERMINATED
. While a
thread is runnable, but before it terminates it can switch
between being
RUNNABLE
and being
BLOCKED
(such as acquiring a
monitor lock),
WAITING
(having invoked
wait
), or
TIMED_WAITING
(having invoked a timed version of
wait
).
public static void
dumpStack()
Prints a stack trace for the current thread on
System.err
.
There are also debugging aids to track the state of a thread group. You
can invoke the following methods on
ThreadGroup
objects to print their
state: