someone uses your class in a thread pool, the behavior might be dangerously wrong.
For example, the users variable in the Operations example above could have just this problemif an
Operations object was used in multiple threads, or different Operations objects were used in the same
thread, the "current user" notion could easily be wrong. Programmers who use Operations objects must
understand this and only use the objects in threading situations that match the assumptions of the class's code.
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 StringtoString()
Returns a string representation of the thread, including its name, its priority,
and the name of its thread group.
public longgetId()
Returns a positive value that uniquely identifies this thread while it is alive.
public Thread.StategetState()
Returns the current state of this thread. Thread.State is a nested 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 voiddumpStack()
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:
public StringtoString()
Returns a string representation of the ThreadGroup, including its name
and priority.
public voidlist()
Lists this ThreadGroup recursively to System.out. This prints the
toString value for each of the threads and thread groups within this group.
I'll play it first and tell you what it is later.
Miles Davis