-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExecutorBasedSimulationRuntime.java
112 lines (96 loc) · 2.6 KB
/
ExecutorBasedSimulationRuntime.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*******************************************************************************************************
*
* ExecutorBasedSimulationRuntime.java, in msi.gama.headless, is part of the source code of the GAMA modeling and
* simulation platform (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package msi.gama.headless.runtime;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import msi.gama.headless.job.ExperimentJob;
import ummisco.gama.dev.utils.DEBUG;
/**
* The Class LocalSimulationRuntime.
*/
public class ExecutorBasedSimulationRuntime implements SimulationRuntime {
static {
DEBUG.ON();
}
/**
* The Class OwnRunnable.
*/
static class OwnRunnable implements Runnable {
/** The sim. */
final ExperimentJob sim;
/**
* Instantiates a new own runnable.
*
* @param s
* the s
*/
OwnRunnable(final ExperimentJob s) {
sim = s;
}
/**
* Run.
*/
@Override
public void run() {
try (final DebugStream file = new DebugStream(sim)) {
sim.loadAndBuild();
sim.playAndDispose();
} catch (final Exception e) {
DEBUG.ERR(e);
}
}
}
/** The executor. */
final ThreadPoolExecutor executor;
/**
* Instantiates a new local simulation runtime.
*/
public ExecutorBasedSimulationRuntime() {
this(UNDEFINED_QUEUE_SIZE);
}
/**
* Sets the number of threads.
*
* @param n
* the new number of threads
*/
@Override
public void setNumberOfThreads(final int n) {
executor.setMaximumPoolSize(n);
}
/**
* Gets the number of threads.
*
* @return the number of threads
*/
public int getNumberOfThreads() { return executor.getMaximumPoolSize(); }
/**
* Instantiates a new local simulation runtime.
*
* @param n
* the number of cores asked
*/
public ExecutorBasedSimulationRuntime(final int n) {
executor = new ThreadPoolExecutor(n, n, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
executor.setRejectedExecutionHandler((r, executor) -> {
if (r instanceof OwnRunnable or) {
DEBUG.ERR("The execution of " + or.sim.getExperimentID() + " has been rejected");
}
});
}
@Override
public void pushSimulation(final ExperimentJob s) {
executor.execute(new OwnRunnable(s));
}
@Override
public boolean isPerformingSimulation() { return executor.getActiveCount() > 0; }
}