11
I've designed a class that fills an array with integers using a various number of threads, in order to see the power of multi threading. But according to my result, there is none...
The idea: The idea was too fill an array of 100000000 integers with the value "1". Starting with 1 thread (one threads fills the whole array) and incrementing it until 100 threads (each thread fills a sub array of size 100000000/nbThreads)
Example: With 10 threads, I create 10 threads and each is filling an array of 10000000 integers.
Here is my code:
public class ThreadedArrayFilling extends Thread{
private int start;
private int partitionSize;
public static int[] data;
public static final int SIZE = 100000000;
public static final int NB_THREADS_MAX = 100;
public static void main(String[] args){
data = new int[SIZE];
long startTime, endTime;
int partition, startIndex, j;
ThreadedArrayLookup[] threads;
for(int i = 1; i <= NB_THREADS_MAX; i++){
startTime = System.currentTimeMillis();
partition = SIZE / i;
startIndex = 0;
threads = new ThreadedArrayLookup[i];
for(j = 0; j < i; j++){
threads[j] = new ThreadedArrayLookup(startIndex, partition);
startIndex += partition;
}
for(j = 0; j < i; j++){
try {
threads[j].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
endTime = System.currentTimeMillis();
System.out.println(i + " THREADS: " + (endTime - startTime) + "ms");
}
}
public ThreadedArrayFilling(int start, int size){
this.start = start;
this.partitionSize = size;
thi