From 5b685e71d3fc625458a95abae8ddf768f1da7b88 Mon Sep 17 00:00:00 2001 From: sk89q Date: Wed, 13 Aug 2014 07:51:13 -0700 Subject: [PATCH] Add ChunkHashTable.awaitCompletion(). --- .../managers/index/ChunkHashTable.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/sk89q/worldguard/protection/managers/index/ChunkHashTable.java b/src/main/java/com/sk89q/worldguard/protection/managers/index/ChunkHashTable.java index 85f90839..7fcb15b2 100644 --- a/src/main/java/com/sk89q/worldguard/protection/managers/index/ChunkHashTable.java +++ b/src/main/java/com/sk89q/worldguard/protection/managers/index/ChunkHashTable.java @@ -40,6 +40,7 @@ import java.util.Collections; import java.util.List; import java.util.Set; +import java.util.concurrent.TimeUnit; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -132,6 +133,25 @@ private void rebuild() { } } + /** + * Waits until all currently executing background tasks complete. + * + * @param timeout the maximum time to wait + * @param unit the time unit of the timeout argument + * @return {@code true} if this executor terminated and + * {@code false} if the timeout elapsed before termination + * @throws InterruptedException on interruption + */ + public boolean awaitCompletion(long timeout, TimeUnit unit) throws InterruptedException { + ListeningExecutorService previousExecutor; + synchronized (lock) { + previousExecutor = executor; + executor = createExecutor(); + } + previousExecutor.shutdown(); + return previousExecutor.awaitTermination(timeout, unit); + } + @Override public void bias(Vector2D chunkPosition) { checkNotNull(chunkPosition);