X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2Finternal%2FThreads.java;h=9e2a4384bcaa4eebf677db4aff9a8d6f3e2f5314;hb=6a6726b1ee9c2e78971d26b50f15b867cc7c5a02;hp=be25de632b990e81742424f6211ec2f4e884b57c;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/internal/Threads.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/internal/Threads.java index be25de632..9e2a4384b 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/internal/Threads.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/internal/Threads.java @@ -1,95 +1,95 @@ -/******************************************************************************* - * Copyright (c) 2007, 2012 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.browsing.ui.swt.internal; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; - -import org.simantics.browsing.ui.swt.Activator; - -/** - * Pooled thread work executor for GraphExplorerImpl internal use only. - * - * Started by {@link Activator}. - * - * @author Tuukka Lehtonen - */ -public final class Threads { - - private static ExecutorService executor; - - public static synchronized void initialize() { - if (executor == null) { - initializeExecutor(); - } - } - - public static synchronized void shutdown() { - if (executor != null) { - executor.shutdown(); - executor = null; - } - } - - private static ExecutorService initializeExecutor() { - if (executor == null) { - final ThreadGroup tg = new ThreadGroup("GraphExplorer-Worker-Group"); - final AtomicInteger counter = new AtomicInteger(0); - ThreadFactory tf = new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread t = new Thread(tg, r, "GraphExplorer-Worker-"+(counter.incrementAndGet())); - if (!t.isDaemon()) - t.setDaemon(true); - if (t.getPriority() != Thread.NORM_PRIORITY) - t.setPriority(Thread.NORM_PRIORITY); - return t; - } - }; - -// int maxPoolSize = Math.min(Math.max(2, ThreadUtils.CORES), 8); - int maxPoolSize = 1; - - if(maxPoolSize == 1) { - - executor = Executors.newSingleThreadExecutor(tf); - - } else { - - executor = - new ThreadPoolExecutor(maxPoolSize / 2, maxPoolSize, - // Don't let the threads die too soon - 30L, TimeUnit.SECONDS, - // LinkedBlockingQueue to allow queueing of tasks - // even when all workers are busy. - new LinkedBlockingQueue(), - tf); - - } - - } - return executor; - } - - public static ExecutorService getExecutor() { - ExecutorService service = executor; - if (service == null) - throw new IllegalStateException("executor not initialized"); - return service; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2012 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.browsing.ui.swt.internal; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.simantics.browsing.ui.swt.Activator; + +/** + * Pooled thread work executor for GraphExplorerImpl internal use only. + * + * Started by {@link Activator}. + * + * @author Tuukka Lehtonen + */ +public final class Threads { + + private static ExecutorService executor; + + public static synchronized void initialize() { + if (executor == null) { + initializeExecutor(); + } + } + + public static synchronized void shutdown() { + if (executor != null) { + executor.shutdown(); + executor = null; + } + } + + private static ExecutorService initializeExecutor() { + if (executor == null) { + final ThreadGroup tg = new ThreadGroup("GraphExplorer-Worker-Group"); + final AtomicInteger counter = new AtomicInteger(0); + ThreadFactory tf = new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(tg, r, "GraphExplorer-Worker-"+(counter.incrementAndGet())); + if (!t.isDaemon()) + t.setDaemon(true); + if (t.getPriority() != Thread.NORM_PRIORITY) + t.setPriority(Thread.NORM_PRIORITY); + return t; + } + }; + +// int maxPoolSize = Math.min(Math.max(2, ThreadUtils.CORES), 8); + int maxPoolSize = 1; + + if(maxPoolSize == 1) { + + executor = Executors.newSingleThreadExecutor(tf); + + } else { + + executor = + new ThreadPoolExecutor(maxPoolSize / 2, maxPoolSize, + // Don't let the threads die too soon + 30L, TimeUnit.SECONDS, + // LinkedBlockingQueue to allow queueing of tasks + // even when all workers are busy. + new LinkedBlockingQueue(), + tf); + + } + + } + return executor; + } + + public static ExecutorService getExecutor() { + ExecutorService service = executor; + if (service == null) + throw new IllegalStateException("executor not initialized"); + return service; + } + +}