问题
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?
回答1:
Is there a better way of doing this?
I doubt it. Your solution looks fine to me, but if you want you can rewrite it into possibly clearer code like:
IntStream.range(0, 100).forEach(
i -> executor.execute(
() -> doSomething(String.format("Need task number %d done", i))
)
);
回答2:
That's the most straightforward way to make it work. You're not going to do better than that.
来源:https://stackoverflow.com/questions/32533621/using-the-non-final-loop-variable-inside-a-lambda-expression