import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ParallelImplementation {
private int numberOfCells;
private double[] h0;
private double[] h1;
private double[] h2;
private double[] h3;
private double[] h4;
private double[] h5;
private double[] h6;
private double[] h7;
private double[] h8;
private double[] h9;
public ParallelImplementation(int numberOfCells) {
this.numberOfCells = numberOfCells;
this.h0 = new double[numberOfCells];
this.h1 = new double[numberOfCells];
this.h2 = new double[numberOfCells];
this.h3 = new double[numberOfCells];
this.h4 = new double[numberOfCells];
this.h5 = new double[numberOfCells];
this.h6 = new double[numberOfCells];
this.h7 = new double[numberOfCells];
this.h8 = new double[numberOfCells];
this.h9 = new double[numberOfCells];
}
public void update() {
final int numberOfThreads = Runtime.getRuntime().availableProcessors();
ExecutorService exec = Executors.newFixedThreadPool(numberOfThreads);
for(int thread = 0; thread < numberOfThreads; thread++) {
final int threadId = thread;
exec.submit(new Runnable() {
@Override
public void run() {
for(int i = threadId; i < numberOfCells; i += numberOfThreads) {
h0[i] = h0[i] + 1;
h1[i] = h1[i] + 1;
h2[i] = h2[i] + 1;
h3[i] = h3[i] + 1;
h4[i] = h4[i] + 1;
h5[i] = h5[i] + 1;
h6[i] = h6[i] + 1;
h7[i] = h7[i] + 1;
h8[i] = h8[i] + 1;
h9[i] = h9[i] + 1;
}
}
});
}
exec.shutdown();
try {
exec.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ParallelImplementation si = new ParallelImplementation(100000);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
if(i % 1000 == 0) {
System.out.println(i);
}
si.update();
}
long stop = System.currentTimeMillis();
System.out.println("Time: " + (stop - start));
}
}