$darkmode
DENOPTIM
StaticTaskManager.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.task;
20
21import java.util.HashMap;
22import java.util.Map;
23import java.util.concurrent.ArrayBlockingQueue;
24import java.util.concurrent.ExecutionException;
25import java.util.concurrent.Future;
26import java.util.concurrent.RejectedExecutionHandler;
27import java.util.concurrent.ThreadPoolExecutor;
28import java.util.concurrent.TimeUnit;
29
30import javax.swing.JProgressBar;
31
32import denoptim.programs.combinatorial.FragSpaceExplorer;
33import denoptim.programs.denovo.GARunner;
34
45{
49 private static final StaticTaskManager instance = new StaticTaskManager();
50
54 private static ThreadPoolExecutor tpe;
55
59 private static Map<Task,Future<?>> subToFutureMap =
60 new HashMap<Task,Future<?>>();
61
66 private static final int maxParallelPrograms = 4;
67
71 private static final int queueSize = 10;
72
76 public static int valProgressBar = 1; //On startup the bar is filled.up.
77
81 public static int maxProgressBar = 1;
82
86 public static JProgressBar queueStatusBar = null;
87
91 public static final String PROGRESSBARUPDATE = "UPDATE-PROGRESS-BAR";
92
96 private static final Object LOCK = new Object();
97
98//------------------------------------------------------------------------------
99
101 {
102 tpe = new ThreadPoolExecutor(maxParallelPrograms, maxParallelPrograms,
103 Long.MAX_VALUE,
104 TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueSize));
105
106 // What to do in case of filled-up resources and queue rejecting tasks
107 tpe.setRejectedExecutionHandler(new RejectedExecutionHandler()
108 {
109 @Override
110 public void rejectedExecution(Runnable r,
111 ThreadPoolExecutor executor)
112 {
113 try
114 {
115 // re-submit to the queue any rejected job
116 executor.getQueue().put(r);
117 }
118 catch (InterruptedException ex)
119 {
120 ex.printStackTrace();
121 }
122 }
123 });
124
125 // What to do in case of shutdown signal
126 Runtime.getRuntime().addShutdownHook(new ShutDownHook());
127
128 tpe.prestartAllCoreThreads();
129 }
130
131//------------------------------------------------------------------------------
132
137 private class ShutDownHook extends Thread
138 {
139 @Override
140 public void run()
141 {
142 tpe.shutdown();
143 try
144 {
145 if (!tpe.awaitTermination(1, TimeUnit.SECONDS))
146 {
147 tpe.shutdownNow(); // Cancel running asks
148 }
149 }
150 catch (InterruptedException ie)
151 {
152 stopAll();
153 subToFutureMap.clear();
154 tpe.purge();
155 tpe.getQueue().clear();
156 tpe.shutdownNow();
157
158 // and stop possibly alive thread
159 //TODO is this needed? It should not
160 Thread.currentThread().interrupt();
161 }
162 }
163 }
164
165//------------------------------------------------------------------------------
166
172 {
173 return instance;
174 }
175
176//------------------------------------------------------------------------------
177
178 public static void addTodoTask()
179 {
180 addTodoTasks(1);
181 }
182
183//------------------------------------------------------------------------------
184
185 public static void addTodoTasks(int addedTasksCount)
186 {
187 synchronized (LOCK) {
188 int max = maxProgressBar;
189 int val = valProgressBar;
190 if (max==1 && val==1)
191 {
192 // NB: the progress bar is initialized to max=1/val=1
193 // Thus it looks "filled-up", i.e., no pending/running task
194 maxProgressBar = addedTasksCount;
195 valProgressBar = 0;
196 } else {
197 maxProgressBar = max+addedTasksCount;
198 valProgressBar = val;
199 }
200 }
201 if (queueStatusBar!=null)
202 {
203 queueStatusBar.setMaximum(maxProgressBar);
205 queueStatusBar.repaint();
206 }
207 }
208
209//------------------------------------------------------------------------------
210
211 public static void subtractDoneTask()
212 {
214 }
215
216//------------------------------------------------------------------------------
217
218 public static void subtractDoneTasks(int doneTasksCount)
219 {
220 synchronized (LOCK) {
221 int val = valProgressBar;
222 valProgressBar = val+doneTasksCount;
224 {
225 maxProgressBar = 1;
226 valProgressBar = 1;
227 }
228 }
229 if (queueStatusBar!=null)
230 {
231 queueStatusBar.setMaximum(maxProgressBar);
233 queueStatusBar.repaint();
234 }
235 }
236
237//------------------------------------------------------------------------------
238
243 public static String getQueueSnapshot()
244 {
245 String s = "Approximate queue status:<ul>"
246 + "<li>Running tasks = " + tpe.getActiveCount() + "/"
247 + tpe.getPoolSize() + "</li>"
248 + "<li>Queue Size = " + tpe.getQueue().size()+ "</li></ul>";
249 return s;
250 }
251
252//------------------------------------------------------------------------------
253
260 public static void submitAndWait(Task task) throws InterruptedException,
261 ExecutionException
262 {
263 tpe.submit(task).get();
264 }
265
266//------------------------------------------------------------------------------
267
268 public static void submit(Task task)
269 {
270 task.setNotify(true);
271 addTodoTask();
272 Future<?> future = tpe.submit(task);
273 subToFutureMap.put(task,future);
274 }
275
276//------------------------------------------------------------------------------
277
278 public static void stop(Task task)
279 {
280 task.stopTask();
281 if (subToFutureMap.containsKey(task))
282 {
283 subToFutureMap.get(task).cancel(true);
284 subToFutureMap.remove(task);
285 }
286 }
287
288//------------------------------------------------------------------------------
289
290 public static void stopAll()
291 {
292 for (Task task : subToFutureMap.keySet())
293 {
294 Future<?> expectation = subToFutureMap.get(task);
295 expectation.cancel(true);
296 task.stopTask();
297 }
298 }
299
300//------------------------------------------------------------------------------
301
302 public static boolean hasActive()
303 {
304 return tpe.getActiveCount() > 0;
305 }
306
307//------------------------------------------------------------------------------
308
314 public static void setLinkToProgressBar(JProgressBar bar)
315 {
316 queueStatusBar = bar;
317 }
318
319//------------------------------------------------------------------------------
320
321}
Shutdown hook that stops all child tasks upon shutdown of JavaVM.
Manager for tasks submitted by the GUI.
static final StaticTaskManager instance
The only, static instance of this class.
static JProgressBar queueStatusBar
Reference to progress bar in GUI.
static final Object LOCK
Synchronisation lock for queue progress bar.
static void submitAndWait(Task task)
Submits a task and waits for completion.
static void subtractDoneTasks(int doneTasksCount)
static void setLinkToProgressBar(JProgressBar bar)
Adds a reference to the given progress bar.
static void addTodoTasks(int addedTasksCount)
static final int queueSize
Queue size.
static final int maxParallelPrograms
Maximum number of denoptim ProgramTasks that can be run in parallel.
static final String PROGRESSBARUPDATE
Property change fired to indicate the need to update progress bar.
static Map< Task, Future<?> > subToFutureMap
Maps the relation between a task that is submitted and its future handle.
static StaticTaskManager getInstance()
Gets the singleton instance of this class.
static ThreadPoolExecutor tpe
The executor of threads.
static int valProgressBar
Value indicated in the progress bar of the GUI.
static int maxProgressBar
Queue max indicator for progress bar in GUI.
A task that can throw exceptions.
Definition: Task.java:30
void setNotify(boolean notify)
Definition: Task.java:217
void stopTask()
Stop the task if not already completed.
Definition: Task.java:177