]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/internal/Threads.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / internal / Threads.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt.internal;\r
13 \r
14 import java.util.concurrent.ExecutorService;\r
15 import java.util.concurrent.Executors;\r
16 import java.util.concurrent.LinkedBlockingQueue;\r
17 import java.util.concurrent.ThreadFactory;\r
18 import java.util.concurrent.ThreadPoolExecutor;\r
19 import java.util.concurrent.TimeUnit;\r
20 import java.util.concurrent.atomic.AtomicInteger;\r
21 \r
22 import org.simantics.browsing.ui.swt.Activator;\r
23 \r
24 /**\r
25  * Pooled thread work executor for GraphExplorerImpl internal use only.\r
26  * \r
27  * Started by {@link Activator}.\r
28  * \r
29  * @author Tuukka Lehtonen\r
30  */\r
31 public final class Threads {\r
32 \r
33     private static ExecutorService     executor;\r
34 \r
35     public static synchronized void initialize() {\r
36         if (executor == null) {\r
37             initializeExecutor();\r
38         }\r
39     }\r
40 \r
41     public static synchronized void shutdown() {\r
42         if (executor != null) {\r
43             executor.shutdown();\r
44             executor = null;\r
45         }\r
46     }\r
47 \r
48     private static ExecutorService initializeExecutor() {\r
49         if (executor == null) {\r
50             final ThreadGroup tg = new ThreadGroup("GraphExplorer-Worker-Group");\r
51             final AtomicInteger counter = new AtomicInteger(0);\r
52             ThreadFactory tf = new ThreadFactory() {\r
53                 @Override\r
54                 public Thread newThread(Runnable r) {\r
55                     Thread t = new Thread(tg, r, "GraphExplorer-Worker-"+(counter.incrementAndGet()));\r
56                     if (!t.isDaemon())\r
57                         t.setDaemon(true);\r
58                     if (t.getPriority() != Thread.NORM_PRIORITY)\r
59                         t.setPriority(Thread.NORM_PRIORITY);\r
60                     return t;\r
61                 }\r
62             };\r
63 \r
64 //            int maxPoolSize = Math.min(Math.max(2, ThreadUtils.CORES), 8);\r
65             int maxPoolSize = 1;\r
66             \r
67             if(maxPoolSize == 1) {\r
68                 \r
69                 executor = Executors.newSingleThreadExecutor(tf);\r
70                 \r
71             } else {\r
72 \r
73                 executor =\r
74                         new ThreadPoolExecutor(maxPoolSize / 2, maxPoolSize,\r
75                                         // Don't let the threads die too soon\r
76                                         30L, TimeUnit.SECONDS,\r
77                                         // LinkedBlockingQueue to allow queueing of tasks\r
78                                         // even when all workers are busy.\r
79                                         new LinkedBlockingQueue<Runnable>(),\r
80                                         tf);\r
81             \r
82             }\r
83             \r
84         }\r
85         return executor;\r
86     }\r
87 \r
88     public static ExecutorService getExecutor() {\r
89         ExecutorService service = executor;\r
90         if (service == null)\r
91             throw new IllegalStateException("executor not initialized");\r
92         return service;\r
93     }\r
94 \r
95 }\r