Java Tools Tutorials - Herong's Tutorial Notes - Version 5.32, by Dr. Herong Yang
PrimeNumberSeeker.java - Multi-Thread Sample Program
This section provides a simple multi-thread Java application, PrimeNumberSeeker.java, which will be used to learn how to debug multi-thread applications.
To help me practice debugging commands, I wrote the following simple multi-thread application, PrimeNumberSeeker.java:
/* PrimeNumberSeeker.java #- Copyright (c) 2015, HerongYang.com, All Rights Reserved. */ public class PrimeNumberSeeker extends Thread { private static final int ceiling = 100; private static final int interval = 1000; private static final int delay = 100; public int count = 0; public int current = 2; public int[] primes = null; public static void main(String[] a) { System.out.println("Period, Current int, # primes"); PrimeNumberSeeker t = new PrimeNumberSeeker(); t.start(); int i = 0; while (true) { i++; System.out.println( i+", "+t.current+", "+t.count); try { sleep(interval); } catch (InterruptedException e) { System.out.println("Monitor interrupted."); } } } public void run() { primes = new int[ceiling]; while (count < ceiling) { current++; int j = 2; boolean isPrime = true; while (j<current/2 && isPrime) { isPrime = current % j > 0; j++; } if (isPrime) { count++; primes[count-1] = current; } try { sleep(delay); } catch (InterruptedException e) { System.out.println("Runner interrupted."); } } } }
Note that:
Here is how I compiled the application and executed without debugging:
C:\herong>javac -g PrimeNumberSeeker.java C:\herong>java PrimeNumberSeeker Period, Current int, # primes 1, 2, 0 2, 12, 5 3, 22, 8 4, 32, 11 5, 42, 13 6, 52, 15 7, 62, 18 8, 72, 20 9, 82, 22 10, 92, 24 11, 102, 26 12, 112, 29 13, 122, 30 ... 53, 521, 98 54, 531, 99 55, 541, 100 56, 541, 100 57, 541, 100 ...
The output seems to be fine. But I want to use "jdb" to exam the calculation and to practice the debugging commands. I will show my debugging session in multiple parts with my comments in sections below.
Last update: 2015.
Table of Contents
'javac' - The Java Program Compiler
'java' - The Java Program Launcher
'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 a 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
'jconsole' - Java Monitoring and Management Console
'jstat' - JVM Statistics Monitoring Tool
jvisualvm (Java VisualVM) - JVM Visual Tool
'javap' - The Java Class File Disassembler
'keytool' - Public Key Certificate Tool