I have an array of arrays of int.
DataArray[X][Y]
I would like to create a thread for each X, which iterates along Y. I cannot figure out how to pass the appropriate X value to each thread.
essentially i would like to be able to do
ExecutorService threadPool = Executors.newFixedThreadPool(10);
for (int i = 0; i < X; i++) {
threadPool.submit(new Runnable() {
public void run() {
Function_to_run(i);
}
});
}
Any help would be appreciated
Only final
values can be captured within a method-local-anonymous-inner-class. You need to change your code as follows :
for (int i = 0; i < X; i++) {
final int index = i;
threadPool.submit(new Runnable() {
public void run() {
Function_to_run(index);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With