当我像这样在地图上进行迭代时,可以安全地调用Thread.currentThread().interrupt()
吗?
for (Map.Entry<Key, List<Future<Value>>> entry : valuesByKey.entrySet()) {
List<Future<Value>> values = entry.getValue();
Key key = entry.getKey();
try {
for (Future<Value> future : results) {
try {
Value value = future.get();
editKey(key, value);
} catch (ExecutionException e) {
LOG.error(e);
} catch (CancellationException e) {
LOG.error(e);
}
}
} catch (InterruptedException e) {
for (Future<?> future : values) {
if (!future.isDone())
// cancel all job threads that haven't been run yet, let existing complete
future.cancel(false);
}
// Restore the interrupted status on the job thread
Thread.currentThread().interrupt();
}
}