I'm trying to execute 100 tasks all in parallel via executors and runnable, the task needs to use the loop variable:
for (int i = 0; i < 100; i++) {
executor.execute(() -> {
doSomething(String.format("Need task number %d done", i));
}
});
}
I get a squiggly under 'i' saying - Variable used in lambda expression should be effectively final.
A loop variable, as far as I'm aware, cannot be made final or effectively final, since it is being changed with each iteration. I found a simple workaround,
for (int i = 0; i < 100; i++) {
int index = i;
executor.execute(() -> {
doSomething(String.format("Need task number %d done", index));
}
});
}
This doesn't seem the most effective solution to me, declaring a new variable at every iteration. Is there a better way of doing this?