aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/util/ThreadHelper.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-11-16 14:30:50 +0300
committerGravatar Vitaly Takmazov2016-11-16 16:21:53 +0300
commitffb24e0d469aaafa35ad5f460a7bdf771382f0ff (patch)
tree0d8fc9114b916f9058cccb01fcf8281a0a209284 /juick-server/src/main/java/com/juick/util/ThreadHelper.java
parente8faf851327a12f28ae664a6f7db6c844d0ba9bf (diff)
server-core -> juick-server
Diffstat (limited to 'juick-server/src/main/java/com/juick/util/ThreadHelper.java')
-rw-r--r--juick-server/src/main/java/com/juick/util/ThreadHelper.java37
1 files changed, 37 insertions, 0 deletions
diff --git a/juick-server/src/main/java/com/juick/util/ThreadHelper.java b/juick-server/src/main/java/com/juick/util/ThreadHelper.java
new file mode 100644
index 00000000..64a56f7c
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/util/ThreadHelper.java
@@ -0,0 +1,37 @@
+package com.juick.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Created by aalexeev on 11/11/16.
+ */
+public class ThreadHelper {
+ private ThreadHelper() {
+ throw new IllegalStateException();
+ }
+
+ private static Logger logger = LoggerFactory.getLogger(ThreadHelper.class);
+
+ public static void shutdownAndAwaitTermination(ExecutorService pool) {
+ pool.shutdown(); // Disable new tasks from being submitted
+ try {
+ // Wait a while for existing tasks to terminate
+ if (!pool.awaitTermination(5, TimeUnit.SECONDS)) {
+ logger.info("Calls shutDownNow for pool");
+ pool.shutdownNow(); // Cancel currently executing tasks
+ // Wait a while for tasks to respond to being cancelled
+ if (!pool.awaitTermination(5, TimeUnit.SECONDS))
+ logger.error("Pool did not terminate");
+ }
+ } catch (InterruptedException ie) {
+ // (Re-)Cancel if current thread also interrupted
+ pool.shutdownNow();
+ // Preserve interrupt status
+ Thread.currentThread().interrupt();
+ }
+ }
+}