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