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