Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.00

Suspending Main Thread to Debug Child Thread

This section provides a tutorial example on how to suspend the main thread to debug only the child thread.

6. Suspending the main thread and keeping only the child thread running:

Thread-0[1] stop at PrimeNumberSeeker:39
Set breakpoint PrimeNumberSeeker:39

Thread-0[1] cont
>
Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=39
39                primes[count-1] = current;

Thread-0[1] cont
>
Breakpoint hit: "thread=main", PrimeNumberSeeker.main(), line=19
19             System.out.println( i+", "+t.current+", "+t.count);

main[1] cont
> 12, 23, 9

Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=39
39                primes[count-1] = current;

Thread-0[1] cont
>
Breakpoint hit: "thread=main", PrimeNumberSeeker.main(), line=19
19             System.out.println( i+", "+t.current+", "+t.count);

main[1] suspend 1

main[1] cont
>
Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=39
39                primes[count-1] = current;

Thread-0[1] cont
>
Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=39
39                primes[count-1] = current;

Thread-0[1] cont
>
Breakpoint hit: "thread=Thread-0", PrimeNumberSeeker.run(), line=39
39                primes[count-1] = current;

Thread-0[1] print current
 current = 41

Thread-0[1] print count
 count = 13

What I have done here:

  • After check different areas of the code, I wanted to only the break the execution when a new prime number is found. So I created a breakpoint at line 39.
  • Then I used "cont" to let the execution to continue. But two threads always executed at the same time, and stopped at the same time whenever one thread reached a breakpoint.
  • So I used "suspend 1" to suspend the main thread. This is a cool command, allowing me to concentrate on a single thread. Of course, you can use "resume 1" to release the suspension.

I think I have done enough debugging practice and want to stop here now. However, my program does have a calculation error. I want to leave it to you to find out.

Sections in This Chapter

'jdb' - Java Debugger Command and Options

Starting a Debugging Session with 'jdb'

Debugging Applications with Separate 'jdb' Sessions

Debugging Java Applications Remotely

Listing Debugging Commands with 'help' Command

PrimeNumberSeeker.java - Multi-Thread Sample Program

Starting Debugging Session on a Multi-Thread Application

Stepping through Statements of the Child Thread

Checking Variable Values in a Debugging Session

Debugging the Main Thread of a Multi-Thread Application

Switching Execution Threads in a Debugging Session

Suspending Main Thread to Debug Child Thread

Dr. Herong Yang, updated in 2008
Suspending Main Thread to Debug Child Thread