diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/multithreading/DSEThreadPool.java')
-rw-r--r-- | Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/multithreading/DSEThreadPool.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/multithreading/DSEThreadPool.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/multithreading/DSEThreadPool.java new file mode 100644 index 00000000..17e96a75 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/multithreading/DSEThreadPool.java | |||
@@ -0,0 +1,58 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package org.eclipse.viatra.dse.multithreading; | ||
10 | |||
11 | import java.util.concurrent.RejectedExecutionException; | ||
12 | import java.util.concurrent.SynchronousQueue; | ||
13 | import java.util.concurrent.ThreadPoolExecutor; | ||
14 | import java.util.concurrent.TimeUnit; | ||
15 | |||
16 | import org.apache.log4j.Logger; | ||
17 | import org.eclipse.viatra.dse.api.DesignSpaceExplorer; | ||
18 | import org.eclipse.viatra.dse.base.ExplorerThread; | ||
19 | |||
20 | /** | ||
21 | * | ||
22 | * @author Andras Szabolcs Nagy | ||
23 | * | ||
24 | */ | ||
25 | public class DSEThreadPool extends ThreadPoolExecutor { | ||
26 | |||
27 | private static final long THREAD_KEEP_ALIVE_IN_SECONDS = 60; | ||
28 | |||
29 | public DSEThreadPool() { | ||
30 | // Based on the Executors.newCachedThreadPool() | ||
31 | super(0, getProcNumber(), THREAD_KEEP_ALIVE_IN_SECONDS, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); | ||
32 | } | ||
33 | |||
34 | // helper for constructor | ||
35 | private static int getProcNumber() { | ||
36 | return Runtime.getRuntime().availableProcessors(); | ||
37 | } | ||
38 | |||
39 | public boolean tryStartNewStrategy(ExplorerThread strategy) { | ||
40 | |||
41 | if (!canStartNewThread()) { | ||
42 | return false; | ||
43 | } | ||
44 | |||
45 | try { | ||
46 | submit(strategy); | ||
47 | } catch (RejectedExecutionException e) { | ||
48 | Logger.getLogger(DesignSpaceExplorer.class).info("Couldn't start new thread.", e); | ||
49 | return false; | ||
50 | } | ||
51 | |||
52 | return true; | ||
53 | } | ||
54 | |||
55 | public boolean canStartNewThread() { | ||
56 | return getMaximumPoolSize() > getActiveCount(); | ||
57 | } | ||
58 | } | ||