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

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:

 1 /**
 2  * PrimeNumberSeeker.java
 3  * Copyright (c) 2003 by Dr. Herong Yang, www.herongyang.com
 4  */
 5 public class PrimeNumberSeeker extends Thread {
 6    private static final int ceiling = 100;
 7    private static final int interval = 1000;
 8    private static final int delay = 100;
 9    public int count = 0;
10    public int current = 2;
11    public int[] primes = null;
12    public static void main(String[] a) {
13       System.out.println("Period, Current int, # primes");
14       PrimeNumberSeeker t = new PrimeNumberSeeker();
15       t.start();
16       int i = 0;
17       while (true) {
18          i++;
19          System.out.println( i+", "+t.current+", "+t.count);
20          try {
21             sleep(interval);
22          } catch (InterruptedException e) {
23             System.out.println("Monitor interrupted.");
24          }
25       }
26    }
27    public void run() {
28       primes = new int[ceiling];
29       while (count < ceiling) {
30          current++;
31          int j = 2;
32          boolean isPrime = true;
33          while (j<current/2 && isPrime) {
34             isPrime = current % j > 0;
35             j++;
36          }
37          if (isPrime) {
38             count++;
39             primes[count-1] = current;
40          }
41          try {
42             sleep(delay);
43          } catch (InterruptedException e) {
44             System.out.println("Runner interrupted.");
45          }
46       }
47    }
48 }

Note that:

  • This application tries to use a sub-thread to calculate prime numbers.
  • The main thread is monitoring how the sub-thread is doing.
  • A delay mechanism is used to slow the calculation.
  • The application has an infinite loop. So you have to terminate it by "Ctrl-C".

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.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing J2SE 1.6.0 on Windows

 Installing J2SE 1.5.0 on Windows

 'javac' - The Java Program Compiler

 'java' - The Java Program Launcher

'jdb' - The Java Debugger

 '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

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
PrimeNumberSeeker.java - Multi-Thread Sample Program