In Java, if multiple threads write asynchronously each to separate sections of a primitive (double) array, and (only) the main thread reads from the array once all threads completed writing.
- Is there a visibility issue, i.e. a chance of reading or writing to a thread-local version of the primitive array?
- If 1. is true, is this solved by adding a memory barrier before the read of the main thread? Which one would you advise using?
Example code below.
Many Thanks in advance, Manuel
//Example code to calculate distance of 1 point to 1mln other points:
double[][] history = this.createRandomMatrix(1000000,8);
double[] order = this.createRandomMatrix(1,8)[0];
double[] result = new double[1000000];
for(int i = 0; i< 100;i++){
pool.execute(new Calculator(history, newPoint, result,i*10000,10000 + i * 10000));
}
pool.shutdown();
try {
pool.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//read happens here
//Calculator run function
public void run() {
for(int i = start;i<end;i++){
result[i] = this.computeDistance(history[i],order);
}
}