|
32 | 32 | import org.slf4j.LoggerFactory;
|
33 | 33 |
|
34 | 34 | import java.util.*;
|
35 |
| -import java.util.concurrent.ExecutorService; |
36 |
| -import java.util.concurrent.TimeUnit; |
| 35 | +import java.util.concurrent.*; |
37 | 36 |
|
38 | 37 | import static com.graphhopper.util.Parameters.CH.DISABLE;
|
39 | 38 |
|
@@ -279,38 +278,41 @@ public void setPreparationThreads(int preparationThreads) {
|
279 | 278 | }
|
280 | 279 |
|
281 | 280 | public void prepare(final StorableProperties properties) {
|
| 281 | + ExecutorCompletionService completionService = new ExecutorCompletionService<>(threadPool); |
282 | 282 | int counter = 0;
|
283 | 283 | for (final PrepareContractionHierarchies prepare : getPreparations()) {
|
284 | 284 | LOGGER.info((++counter) + "/" + getPreparations().size() + " calling CH prepare.doWork for " + prepare.getWeighting() + " ... (" + Helper.getMemInfo() + ")");
|
285 | 285 | final String name = AbstractWeighting.weightingToFileName(prepare.getWeighting());
|
286 |
| - threadPool.execute(new Runnable() { |
| 286 | + completionService.submit(new Runnable() { |
287 | 287 | @Override
|
288 | 288 | public void run() {
|
289 | 289 | String errorKey = CH.PREPARE + "error." + name;
|
290 | 290 | try {
|
291 | 291 | // toString is not taken into account so we need to cheat, see http://stackoverflow.com/q/6113746/194609 for other options
|
292 | 292 | Thread.currentThread().setName(name);
|
293 |
| - |
294 | 293 | properties.put(errorKey, "CH preparation incomplete");
|
295 | 294 | prepare.doWork();
|
296 | 295 | properties.remove(errorKey);
|
297 | 296 | properties.put(CH.PREPARE + "date." + name, Helper.createFormatter().format(new Date()));
|
298 | 297 | } catch (Exception ex) {
|
299 | 298 | LOGGER.error("Problem while CH preparation " + name, ex);
|
300 | 299 | properties.put(errorKey, ex.getMessage());
|
| 300 | + throw ex; |
301 | 301 | }
|
302 | 302 | }
|
303 |
| - }); |
| 303 | + }, name); |
| 304 | + |
304 | 305 | }
|
305 | 306 |
|
306 | 307 | threadPool.shutdown();
|
307 |
| - try { |
308 |
| - if (!threadPool.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS)) |
309 |
| - threadPool.shutdownNow(); |
310 | 308 |
|
311 |
| - } catch (InterruptedException ie) { |
| 309 | + try { |
| 310 | + for (int i = 0; i < getPreparations().size(); i++) { |
| 311 | + completionService.take().get(); |
| 312 | + } |
| 313 | + } catch (Exception e) { |
312 | 314 | threadPool.shutdownNow();
|
313 |
| - throw new RuntimeException(ie); |
| 315 | + throw new RuntimeException(e); |
314 | 316 | }
|
315 | 317 | }
|
316 | 318 |
|
|
0 commit comments