I really do not have access to anyone, outside of professors, that is highly knowledgeable about Java so I figured I would post my stuff here to further my knowledge and improve my coding. This is homework for my Operating Systems class and it works. Here are some of my main concerns.
- Is this a good use of the static inner class?
- Are my variable and class declarations properly declared?
- Is there a better way than using Thread.sleep to make the sure that the main thread waits for certain child threads to complete before it performs some action?
The object of the program is to find the max in an array using 3 steps. Each step is to perform one part of the task with its own thread. For example, step one uses n threads to initialize an array of size n with 1's. Input done via command line: Prog1.java n x0 x1 ... xn, where n is the number of elements and x0 x1 ... xn are the elements. Input is limited to about 20 elements.
As I am a student, I will also glady accept any critiques on any part of the code. Sorry if this seems like too much for one question and thanks in advance to anyone who contributes.
/* User Input: 4 3 1 7 5
Number of input values = 4
Input values x = 3 1 7 5
After initialization w = 1 1 1 1
Thread T(0,1) compares x[0] = 3 and x[1] = 1, and writes 0 into w[1]
Thread T(2,3) compares x[2] = 7 and x[3] = 5, and writes 0 into w[3]
Thread T(1,3) compares x[1] = 1 and x[3] = 5, and writes 0 into w[1]
Thread T(1,2) compares x[1] = 1 and x[2] = 7, and writes 0 into w[1]
Thread T(0,3) compares x[0] = 3 and x[3] = 5, and writes 0 into w[0]
Thread T(0,2) compares x[0] = 3 and x[2] = 7, and writes 0 into w[0]
After Step 2 w = 0 0 1 0
Maximum = 7
Location = 2
*/
import java.util.Arrays;
public class Prog1 {
private static int[] w;
private static int[] x;
public static void main(String[] args) throws InterruptedException {
if (args.length == 0) { /* return if there are NO arguments */
System.out.println("No arguments!");
return;
}
int size = Integer.parseInt(args[0]); /* Get the number of args */
x = new int[size]; /* Parse string array to integer */
for (int i = 0; i < size; i++) {
try {
x[i] = Integer.parseInt(args[i + 1]);
} catch (NumberFormatException nfe) {
System.out.println("Conversion Error: " + nfe.getMessage());
return;
}}
/* Initialize all w elements to 1 with a thread for each element */
w = new int[size];
for (int i = 0; i < size; i++) {
Initialize t = new Initialize(i);
t.start();
}
System.out.printf("%-25s%d\n","Number of input values = ", size);
String input = Arrays.toString(x).replace("[", "").
replace("]", "").replace(",", "");
System.out.printf("%-21s%s\n","Input values ", "x = " + input);
String init = Arrays.toString(w).replace("[", "").
replace("]", "").replace(",", "");
System.out.printf("%-21s%s\n","After initialization ", "w = " + init);
/* Perform Comparisons with a thread for each comparison */
int inc = 1;
for (int i = 0; i < size - 1; i++) {
for (int j = inc; j < size; j++) {
FindMax t = new FindMax(i,j);
t.start();
}
inc++;
}
Thread.sleep(100); /* After Step 2 Output */
String maxFound = Arrays.toString(w).replace("[", "").
replace("]", "").replace(",", "");
System.out.printf("%-21s%s\n","After Step 2 ","w = " + maxFound);
/* Print the maximum number and its location with a thread for each element*/
for (int i = 0; i < size; i++) {
PrintMax t = new PrintMax(i);
t.start();
}
} // End main()
private static class Initialize extends Thread {
private final int value = 1;
private final int index;
public Initialize(int i) {
this.index = i;
}
@Override
public void run() {
w[index] = this.value;
}}
private static class FindMax extends Thread {
private final int indI;
private final int indJ;
public FindMax(int indI, int indJ) {
this.indI = indI;
this.indJ = indJ;
}
@Override
public void run() {
int num1 = x[indI];
int num2 = x[indJ];
int lesser;
int compare = Integer.compare(num1, num2);
if (compare < 0) {
w[indI] = 0;
lesser = indI;
} else {
w[indJ] = 0;
lesser = indJ;
}
System.out.printf("Thread T(%d,%d) compares x[%1$d] = %d and x[%2$d] = %d, "
+ "and writes 0 into w[%d]\n", indI, indJ, num1, num2, lesser);
}
}
static class PrintMax extends Thread {
private final int index;
public PrintMax(int index) {
this.index = index;
}
@Override
public void run() {
if (w[index] == 1) {
int max = x[index];
System.out.printf("%-23s%s%d\n", "Maximum", "= ", max);
System.out.printf("%-23s%s%d\n", "Location", "= ", index);
}
}
}}