How do I parallelize a while loop?

I am trying to parallelize a while loop in Julia but I am not sure where to start.

For example, suppose I have the following loop:

A = []
i = 0
while i < 5000000
	j = rand(1:5000000) 
	if j == i 
		push!(A,i)
		i = 0
	else
		i += 1
	end
end

And suppose that I want to run this while loop 1000 times, each time updating the same vector A. How can I parallelize this task?

I’m not familiar with how, but SO had a question similar to this. There is an illuminating comment at the bottom by @ChrisRackauckas as well.

Also here on this board.
And a recent presentation at UIC.

Where to start: Multi-Threading · The Julia Language

(and then, it is better if you provide a more realistic example of what you want to do)

If you want multiple threads to push to the same array A in parallel, be aware that you’ll need to use some kind of lock. Alternatively, each thread could push to a different array, and then you could merge them at the end.

Of course, if your code really looks similar to the above, you can probably save a lot of time by careful use of statistics. It might be instructive to look at the implementation of randsubseq to see how it gains efficiency by not iterating over every element, but instead by sampling from the probability distribution on the number of iterations between one push! and the next. You can probably do something similar here.

PS. A = [] allocates an array of Any, which is an abstractly typed container. You probably want A = Int[] so that Julia knows that the elements are Int.