diff --git a/core/src/main/java/com/graphhopper/routing/ch/CHAlgoFactoryDecorator.java b/core/src/main/java/com/graphhopper/routing/ch/CHAlgoFactoryDecorator.java index d3abdd4d071..617a7dd4a07 100644 --- a/core/src/main/java/com/graphhopper/routing/ch/CHAlgoFactoryDecorator.java +++ b/core/src/main/java/com/graphhopper/routing/ch/CHAlgoFactoryDecorator.java @@ -32,8 +32,7 @@ import org.slf4j.LoggerFactory; import java.util.*; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; import static com.graphhopper.util.Parameters.CH.DISABLE; @@ -279,18 +278,18 @@ public void setPreparationThreads(int preparationThreads) { } public void prepare(final StorableProperties properties) { + ExecutorCompletionService completionService = new ExecutorCompletionService<>(threadPool); int counter = 0; for (final PrepareContractionHierarchies prepare : getPreparations()) { LOGGER.info((++counter) + "/" + getPreparations().size() + " calling CH prepare.doWork for " + prepare.getWeighting() + " ... (" + Helper.getMemInfo() + ")"); final String name = AbstractWeighting.weightingToFileName(prepare.getWeighting()); - threadPool.execute(new Runnable() { + completionService.submit(new Runnable() { @Override public void run() { String errorKey = CH.PREPARE + "error." + name; try { // toString is not taken into account so we need to cheat, see http://stackoverflow.com/q/6113746/194609 for other options Thread.currentThread().setName(name); - properties.put(errorKey, "CH preparation incomplete"); prepare.doWork(); properties.remove(errorKey); @@ -298,19 +297,22 @@ public void run() { } catch (Exception ex) { LOGGER.error("Problem while CH preparation " + name, ex); properties.put(errorKey, ex.getMessage()); + throw ex; } } - }); + }, name); + } threadPool.shutdown(); - try { - if (!threadPool.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS)) - threadPool.shutdownNow(); - } catch (InterruptedException ie) { + try { + for (int i = 0; i < getPreparations().size(); i++) { + completionService.take().get(); + } + } catch (Exception e) { threadPool.shutdownNow(); - throw new RuntimeException(ie); + throw new RuntimeException(e); } } diff --git a/core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java b/core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java index 1751c6c6920..34eead5307d 100644 --- a/core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java +++ b/core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java @@ -304,6 +304,11 @@ void contractNodes() { CHEdgeIterator iter = vehicleAllExplorer.setBaseNode(polledNode); while (iter.next()) { + + if(Thread.currentThread().isInterrupted()){ + throw new RuntimeException("Thread was interrupted"); + } + int nn = iter.getAdjNode(); if (prepareGraph.getLevel(nn) != maxLevel) continue;