for my university course we have just started parallel programming, however, I have been given the task of working out how long it takes to perform the task in parallel. Now I do already know that I need to use the instance of "System.currentTimeMillis();" to ensure this works, and I will also need to implement this before the task is performed, and after, to determine how long it has taken for it to initialise. This has greatly confused me as every time I try to implement this, I get exactly the same timings and I don't think I'm implementing it correctly. any help would be greatly appreciated as I still can't seem to get the 'click' with Java.
Sorry if I haven't formatted things correctly etc.
Here is my main class:
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
// Define a new array
ArrayList<Integer> L = new ArrayList<Integer>(100);
// Initialise the array with random values between 0 and 100
Random R = new Random();
for (Integer i=0; i<100; i++) {
L.add(R.nextInt(10));
}
for (Integer i=0; i<100; i++) {
System.out.print(L.get(i));
System.out.print(", ");
}
System.out.println("");
// Process the array
Thread doubler1 = new Doubler(L, 0, 50);
Thread doubler2 = new Doubler(L, 50, 100);
doubler1.start();
doubler2.start();
try {
doubler1.join();
doubler2.join();
}
catch(InterruptedException e) {}
for (Integer i=0; i<100; i++) {
System.out.print(L.get(i));
System.out.print(", ");
}
}
}
And here is my Doubler class:
import java.util.*;
import java.lang.*;
public class Doubler extends Thread {
// We'll store a reference to the list we want to process
// (all instances could see the same list)
private ArrayList<Integer> L;
// Store the range within the array we want to process
private int startIndex, endIndex;
// Constructor to initialise Doubler instance - this stores a reference to the list to process
// and the start and end indices to process when the thread is run.
public Doubler(ArrayList<Integer> list, int start, int end) {
L = list;
startIndex = start;
endIndex = end;
}
// Run this on a thread - managed by the JRE
public void run() {
// Process the range of integers by doubling each value
for (int i=startIndex;i<endIndex;i++) {
L.set(i, L.get(i) *2);
}
}
}