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
*/
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");
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:

• This application tries to use a sub-thread to calculate prime numbers.
• 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

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.