This question has been asked again and again, but I still doubt it. When people say that synchronized creates a memory barrier, what does this memory barrier refer to, ANY cached variable? It does not look doable.
So, because of this doubt, I wrote code that looks like this:
final AtomicReferenceArray<Double> total=new AtomicReferenceArray<Double>(func.outDim); for(int i=0; i<func.outDim; i++) total.set(i, 0.); for(int i=0; i<threads; i++){ workers[i]=new Thread(new Runnable(){ public void run() { double[] myPartialSum=new double(func.outDim);
I wonder if it is possible to simply substitute the type total with a simple double []: this will require that the synchronized (common) (in the run () method) ensures that I do not work with local copies of each index in the doubles array, i.e. memory sampling does not apply only to the total value itself (located under the hood of the pointer), but also to the total indices. It happens?
source share