0

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);
    }
}

}
  • Don't use `System.currentTimeMillis()` to measure elapsed time. See http://stackoverflow.com/questions/1770010/how-do-i-measure-time-elapsed-in-java. – Andy Turner Oct 21 '16 at 20:57
  • Add the code where you tried it yourself and explain **why** you think your results are incorrect. – Tom Oct 21 '16 at 20:57
  • I have been instructed to use the System.currentTimeMillis());, it doesn't matter if the system clock is slightly off – R0ckTillWeDr0p Oct 21 '16 at 21:14
  • I have tried adding "System.out.print("Current Time in milliseconds = "); System.out.println(System.currentTimeMillis());" around the doubler1.join and the doubler2.join however it just shows the results of "Current Time in milliseconds = 1477084361774 Current Time in milliseconds = 1477084361774" After the first instance is ran and before the second instance is ran – R0ckTillWeDr0p Oct 21 '16 at 21:14
  • So the most important insight here might be: **Computers are fast**. Yeah, pretty darn fast. You could try using lists of, say, 1 Million elements, and see whether you see a difference then (but even then, this may be difficult - not to mention all the caveats of "microbenchmarking" in Java...) – Marco13 Oct 21 '16 at 22:27
  • How would I modify the code to process the array using 4 threads instead of 2? I have tried adding new doubler but this doesn't print it, still only prints two lines. – R0ckTillWeDr0p Oct 23 '16 at 21:23

1 Answers1

0

System.currentTimeMillis() may return the same time if two calls follow too closely. Since you are constrained to measuring time this way, you need to change the threads to make them perform a lengthier task so that the time may be accurately measured.

Daniel O
  • 336
  • 2
  • 3
  • 8