当我迭代未来的地图时,我可以调用Thread.currentThread()。interrupt()吗?

时间:2017-11-02 10:34:47

标签: java multithreading

当我像这样在地图上进行迭代时,可以安全地调用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();
        }
    }

0 个答案:

没有答案