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:
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.