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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt.internal;
\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
22 import org.simantics.browsing.ui.swt.Activator;
\r
25 * Pooled thread work executor for GraphExplorerImpl internal use only.
\r
27 * Started by {@link Activator}.
\r
29 * @author Tuukka Lehtonen
\r
31 public final class Threads {
\r
33 private static ExecutorService executor;
\r
35 public static synchronized void initialize() {
\r
36 if (executor == null) {
\r
37 initializeExecutor();
\r
41 public static synchronized void shutdown() {
\r
42 if (executor != null) {
\r
43 executor.shutdown();
\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
54 public Thread newThread(Runnable r) {
\r
55 Thread t = new Thread(tg, r, "GraphExplorer-Worker-"+(counter.incrementAndGet()));
\r
58 if (t.getPriority() != Thread.NORM_PRIORITY)
\r
59 t.setPriority(Thread.NORM_PRIORITY);
\r
64 // int maxPoolSize = Math.min(Math.max(2, ThreadUtils.CORES), 8);
\r
65 int maxPoolSize = 1;
\r
67 if(maxPoolSize == 1) {
\r
69 executor = Executors.newSingleThreadExecutor(tf);
\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
88 public static ExecutorService getExecutor() {
\r
89 ExecutorService service = executor;
\r
90 if (service == null)
\r
91 throw new IllegalStateException("executor not initialized");
\r