aboutsummaryrefslogtreecommitdiff
path: root/juick-core/src/main/java/com/juick/util
diff options
context:
space:
mode:
authorGravatar Alexander Alexeev2016-11-12 01:34:07 +0700
committerGravatar Vitaly Takmazov2016-11-12 11:14:56 +0300
commit194a9a763c6b11d207e682b2f93de94475c473b4 (patch)
tree196317662017b716066eadfa413ecbdc7532c52d /juick-core/src/main/java/com/juick/util
parent7d86347a351aa5263c19cd03aa0bda4650c8bfca (diff)
extracted application configuration from Mvc configuration with application specific beans;
extracted bean initialization from constructor; force using properties;
Diffstat (limited to 'juick-core/src/main/java/com/juick/util')
-rw-r--r--juick-core/src/main/java/com/juick/util/ThreadHelper.java36
1 files changed, 36 insertions, 0 deletions
diff --git a/juick-core/src/main/java/com/juick/util/ThreadHelper.java b/juick-core/src/main/java/com/juick/util/ThreadHelper.java
new file mode 100644
index 00000000..7304d158
--- /dev/null
+++ b/juick-core/src/main/java/com/juick/util/ThreadHelper.java
@@ -0,0 +1,36 @@
+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)) {
+ 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();
+ }
+ }
+}