This section provides a tutorial example on how to start a debugging session on a multi-thread Java application, PrimeNumberSeeker.java.
1. Setting up breakpoints and getting the debugging session going:
C:\herong>jdb PrimeNumberSeeker
Initializing jdb ...
> stop in PrimeNumberSeeker.main
Deferring breakpoint PrimeNumberSeeker.main.
It will be set after the class is loaded.
> run
run PrimeNumberSeeker
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint PrimeNumberSeeker.main
Breakpoint hit: "thread=main", PrimeNumberSeeker.main(), line=13
13 System.out.println("Period, Current int, # primes");
main[1] stop in PrimeNumberSeeker.run
Set breakpoint PrimeNumberSeeker.run
main[1] cont
Period, Current int, # primes
1, 2, 0
>
Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=28
28 primes = new int[ceiling];
Thread-0[1] threads
Group system:
(java.lang.ref.Reference$ReferenceHandler)0xe2 Reference Handler
(java.lang.ref.Finalizer$FinalizerThread)0xe1 Finalizer
(java.lang.Thread)0xe0 Signal Dispatcher
Group main:
(java.lang.Thread)0x1 main running
(PrimeNumberSeeker)0x118 Thread-0 running (at breakpoint)
Ok. What I have done so far:
Started the debugging session with the first breakpoint at the beginning
of the main() method, which cause the execution to stop at line 13.
Then I created the second breakpoint at the beginning of the run() method
in order to catch the sub thread.
Then I issued the "cont" command. My application continued with the sub
thread created stopped at the breakpoint at line 28. The main thread also stopped.
Noticed that the debugger prompt is changed from "main[1]" to "Thread-0[1]".
This is to inform you that you are currently in the sub thread, no longer in
the main thread.
Then I used the "threads" command to list all threads. I saw two thread groups:
"system" and "main". Of course, I am not interested in the "system" group at this point.
The "main" shows two threads: "main" (my monitoring thread) and "Thread-0" (my sub thread
working on the calculation).
Thread "Thread-0" status shows "running (at breakpoint)". But the word "running" is referring
to the thread execution mode, not the current execution status.
Notice that tread "main" is also stopped at this moment. As a general rule, if one thread
is stopped, all other threads are stopped also.