Array changes not seen (by other threads?)

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error Array changes not seen (by other threads?) on this date .

I have a recursive function which performs the following things(among others), in that order:

  1. Prints the array A which is passed as a parameter
  2. Concatenates some new values into it: A=A.concat(localList);
  3. Prints the array A again
  4. Runs a for loop, each iteration of which calls the function again

While the print sandwich shows correct concatenation, I notice that different(parallel?) instances do not aknowledge the changes other make. Aren’t arrays passed as reference?

I’ve included minimal info because I feel this is some basic fact I’m missing.

Answer

Array.concat returns a new instance. In order to keep the reference intact, you can concat a list like so:

yourarray.push.apply(yourarray, newitems)

Or more modern variant:

yourarray.push(...newitems)

To be clear, this will still not work if you’re using multiple threads (Workers) since objects passed between Workers are cloned.