Skip to content
Advertisement

How do I pass different value to each thread in ExecutorService?

Lets say I have an array num_array = [0,1,2,3,4...30]

and I have a multi-threading function like this

JavaScript

I wanted to know how I can pass num_array[0] to thread 1 and num_array[1] to thread 2num_array[24] to thread 25

Advertisement

Answer

Just loop over the array and pass each runnable the element:

JavaScript

Note that you should not assume the order of elements will correspond to the order of threads created in the pool. That’s implementation dependent.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement