--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt.internal;\r
+\r
+import java.util.concurrent.ExecutorService;\r
+import java.util.concurrent.Executors;\r
+import java.util.concurrent.LinkedBlockingQueue;\r
+import java.util.concurrent.ThreadFactory;\r
+import java.util.concurrent.ThreadPoolExecutor;\r
+import java.util.concurrent.TimeUnit;\r
+import java.util.concurrent.atomic.AtomicInteger;\r
+\r
+import org.simantics.browsing.ui.swt.Activator;\r
+\r
+/**\r
+ * Pooled thread work executor for GraphExplorerImpl internal use only.\r
+ * \r
+ * Started by {@link Activator}.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class Threads {\r
+\r
+ private static ExecutorService executor;\r
+\r
+ public static synchronized void initialize() {\r
+ if (executor == null) {\r
+ initializeExecutor();\r
+ }\r
+ }\r
+\r
+ public static synchronized void shutdown() {\r
+ if (executor != null) {\r
+ executor.shutdown();\r
+ executor = null;\r
+ }\r
+ }\r
+\r
+ private static ExecutorService initializeExecutor() {\r
+ if (executor == null) {\r
+ final ThreadGroup tg = new ThreadGroup("GraphExplorer-Worker-Group");\r
+ final AtomicInteger counter = new AtomicInteger(0);\r
+ ThreadFactory tf = new ThreadFactory() {\r
+ @Override\r
+ public Thread newThread(Runnable r) {\r
+ Thread t = new Thread(tg, r, "GraphExplorer-Worker-"+(counter.incrementAndGet()));\r
+ if (!t.isDaemon())\r
+ t.setDaemon(true);\r
+ if (t.getPriority() != Thread.NORM_PRIORITY)\r
+ t.setPriority(Thread.NORM_PRIORITY);\r
+ return t;\r
+ }\r
+ };\r
+\r
+// int maxPoolSize = Math.min(Math.max(2, ThreadUtils.CORES), 8);\r
+ int maxPoolSize = 1;\r
+ \r
+ if(maxPoolSize == 1) {\r
+ \r
+ executor = Executors.newSingleThreadExecutor(tf);\r
+ \r
+ } else {\r
+\r
+ executor =\r
+ new ThreadPoolExecutor(maxPoolSize / 2, maxPoolSize,\r
+ // Don't let the threads die too soon\r
+ 30L, TimeUnit.SECONDS,\r
+ // LinkedBlockingQueue to allow queueing of tasks\r
+ // even when all workers are busy.\r
+ new LinkedBlockingQueue<Runnable>(),\r
+ tf);\r
+ \r
+ }\r
+ \r
+ }\r
+ return executor;\r
+ }\r
+\r
+ public static ExecutorService getExecutor() {\r
+ ExecutorService service = executor;\r
+ if (service == null)\r
+ throw new IllegalStateException("executor not initialized");\r
+ return service;\r
+ }\r
+\r
+}\r