3 ms·
Java gets this right. for (int i : someCollection) { pool.execute(() -> something(i)); } Because "i" is effectively final, each Runnable closes over
by erik_seaberg 9y ago
Java gets this right.
for (int i : someCollection) {
pool.execute(() -> something(i));
}
Because "i" is effectively final, each Runnable closes over the correct value for that iteration. If "i" were being reassigned, the closure wouldn't be allowed to use it, you'd have to decide whether to close over a final copy or a reference to a mutable object that holds the latest value over time (a one-element array is the idiom).