Java Reference
In-Depth Information
system continue to operate normally, doing their thing in the background. Step-
ping in this case only affects the suspended thread. For example, if you're debug-
ging a web application, only the current request thread is suspended, allowing
the server to continue to process requests on its other threads.
Sometimes, however, this behavior can lead to problems, confusion, or worse,
because threads may have dependencies on each other that may negatively affect
the system. The
Suspend All Threads While Stepping
option lets you simulta-
neously suspend all of the threads in the system any time a breakpoint is
reached.
Each time you step, all the threads continue, suspending again when
the step is completed.
This is a good way to prevent background threads from affecting the state of
the application you're trying to examine. We'll illustrate the usefulness of this fea-
ture with a true story: While debugging a web application, we kept finding our-
selves logged out of the application by the time we finished tracing a request.
Finally, we discovered that a background thread designed to prevent stale sessions
was kicking us off the system after 5 minutes of inactivity! Suspending all the
threads together fixed the problem, allowing us to debug in peace.
Suspending individual threads on demand
If you don't have the
Suspend All Threads While Stepping
option enabled, you
can still suspend individual threads as needed. In the
Threads
tab of the
Debug
window, right-click an active thread and select the
Freeze
option from the context
menu. The thread will now be suspended. Right-clicking a suspended thread
gives you the option of resuming that thread independently of the rest. The only
way to return this thread to its processing chores is to select the
Resume
option
from the thread's context menu.
Issuing collective
Resume
commands won't include individually sus-
pended threads.
WARNING
Exporting information about your threads
To obtain more information about your program's threads, select the
Export
Threads
option from the
Run
menu or from the context menu in the
Threads
tab of the
Debug
window. This option creates a report on the status of all the
threads; you can save the report to a text file, copy it to the clipboard, or view it
onscreen. This is the same information you get when you invoke a thread dump
from the
VM
by pressing
Ctrl+Break
on Windows systems or
Ctrl+\
on Linux
systems. Invoking a thread dump doesn't affect the
JVM
, and it will continue to