Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions core/src/main/java/org/elasticsearch/threadpool/ThreadPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -679,14 +679,23 @@ static final class Fields {
public static boolean terminate(ExecutorService service, long timeout, TimeUnit timeUnit) {
if (service != null) {
service.shutdown();
try {
if (service.awaitTermination(timeout, timeUnit)) {
return true;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
if (awaitTermination(service, timeout, timeUnit)) return true;
service.shutdownNow();
return awaitTermination(service, timeout, timeUnit);
}
return false;
}

private static boolean awaitTermination(
final ExecutorService service,
final long timeout,
final TimeUnit timeUnit) {
try {
if (service.awaitTermination(timeout, timeUnit)) {
return true;
}
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}
Expand All @@ -699,22 +708,31 @@ public static boolean terminate(ThreadPool pool, long timeout, TimeUnit timeUnit
if (pool != null) {
try {
pool.shutdown();
try {
if (pool.awaitTermination(timeout, timeUnit)) {
return true;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
if (awaitTermination(pool, timeout, timeUnit)) return true;
// last resort
pool.shutdownNow();
return awaitTermination(pool, timeout, timeUnit);
} finally {
IOUtils.closeWhileHandlingException(pool);
}
}
return false;
}

private static boolean awaitTermination(
final ThreadPool pool,
final long timeout,
final TimeUnit timeUnit) {
try {
if (pool.awaitTermination(timeout, timeUnit)) {
return true;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}

@Override
public void close() throws IOException {
threadContext.close();
Expand Down