Accessing the parallel array through the executing service

Are array elements correctly published between workers?

Suppose I have a large array (of any type of atomic data, therefore not long or double ),

  • I create a worker that fills the array, which I pass it to the contructor,
  • I transfer the employee to the executor and wait for it to be completed (for example, using future.get() ). The worker returns nothing. It just populates my array.
  • Then I immediately create and send another worker with the same contructor array in it. Does he see the latest meanings?
In other words, did it guarantee that the last record of the previous employee would occur before the first reading of the next employee?

Should I instead (or for best practice or something else) allow the first worker to return the array, even if the link is the same as I already have?

[Edit] Some prerequisites: I use byte arrays or short arrays that represent images and use up to 500,000,000 elements each. I am doing simple arithmetic for each element.

+5
source share
2 answers

From the java.util.concurrent JavaDoc package :

The methods of all classes in java.util.concurrent and its subpackages extend these guarantees for higher-level synchronization. In particular:

Actions performed by asynchronous computation represented by future events before actions after receiving the result via Future.get () in another thread.

Actions in the thread before the Runnable submission to the Executor occur before its execution begins. Similarly for Callables passed to ExecutorService.

Accordingly, accessing the array from the second worker in your script seems pretty safe.

+3
source

The elements of the array are not volatile , so they can be cached in the stream by the processor. Therefore, the first worker can initialize some element of the array, but for the second worker not to see it due to caching.

To make sure that the elements of the array themselves are atomic, you can use AtomicReferenceArray

+2
source

All Articles