Skip to content

Commit 2c34c35

Browse files
authored
Added ExecutorService for Landmarks (graphhopper#995)
1 parent 5c15c03 commit 2c34c35

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

core/src/main/java/com/graphhopper/routing/lm/LMAlgoFactoryDecorator.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141

4242
import java.io.IOException;
4343
import java.util.*;
44+
import java.util.concurrent.ExecutorCompletionService;
4445
import java.util.concurrent.ExecutorService;
45-
import java.util.concurrent.TimeUnit;
4646

4747
import static com.graphhopper.util.Parameters.Landmark.DISABLE;
4848

@@ -264,6 +264,7 @@ public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) {
264264
* @see com.graphhopper.routing.ch.CHAlgoFactoryDecorator#prepare(StorableProperties) for a very similar method
265265
*/
266266
public boolean loadOrDoWork(final StorableProperties properties) {
267+
ExecutorCompletionService completionService = new ExecutorCompletionService<>(threadPool);
267268
int counter = 0;
268269
boolean prepared = false;
269270
for (final PrepareLandmarks plm : preparations) {
@@ -274,7 +275,7 @@ public boolean loadOrDoWork(final StorableProperties properties) {
274275
prepared = true;
275276
LOGGER.info(counter + "/" + getPreparations().size() + " calling LM prepare.doWork for " + plm.getWeighting() + " ... (" + Helper.getMemInfo() + ")");
276277
final String name = AbstractWeighting.weightingToFileName(plm.getWeighting());
277-
threadPool.execute(new Runnable() {
278+
completionService.submit(new Runnable() {
278279
@Override
279280
public void run() {
280281
String errorKey = Landmark.PREPARE + "error." + name;
@@ -289,17 +290,17 @@ public void run() {
289290
properties.put(errorKey, ex.getMessage());
290291
}
291292
}
292-
});
293+
}, name);
293294
}
294295

295296
threadPool.shutdown();
296297
try {
297-
if (!threadPool.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS))
298-
threadPool.shutdownNow();
299-
300-
} catch (InterruptedException ie) {
298+
for(int i = 0; i < getPreparations().size(); i++){
299+
completionService.take().get();
300+
}
301+
} catch (Exception e) {
301302
threadPool.shutdownNow();
302-
throw new RuntimeException(ie);
303+
throw new RuntimeException(e);
303304
}
304305
return prepared;
305306
}

core/src/main/java/com/graphhopper/routing/lm/LandmarkStorage.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ private boolean createLandmarksForSubnetwork(final int startNode, final byte[] s
372372
// 1b) we have one landmark, now determine the other landmarks
373373
tmpLandmarkNodeIds[0] = explorer.getLastNode();
374374
for (int lmIdx = 0; lmIdx < tmpLandmarkNodeIds.length - 1; lmIdx++) {
375+
if(Thread.currentThread().isInterrupted()){
376+
throw new RuntimeException("Thread was interrupted");
377+
}
375378
explorer = new LandmarkExplorer(graph, this, initWeighting, traversalMode);
376379
explorer.setFilter(blockedEdges, true, true);
377380
// set all current landmarks as start so that the next getLastNode is hopefully a "far away" node
@@ -389,6 +392,9 @@ private boolean createLandmarksForSubnetwork(final int startNode, final byte[] s
389392

390393
// 2) calculate weights for all landmarks -> 'from' and 'to' weight
391394
for (int lmIdx = 0; lmIdx < tmpLandmarkNodeIds.length; lmIdx++) {
395+
if(Thread.currentThread().isInterrupted()){
396+
throw new RuntimeException("Thread was interrupted");
397+
}
392398
int lmNodeId = tmpLandmarkNodeIds[lmIdx];
393399
LandmarkExplorer explorer = new LandmarkExplorer(graph, this, weighting, traversalMode);
394400
explorer.initFrom(lmNodeId, 0);

0 commit comments

Comments
 (0)