41
41
42
42
import java .io .IOException ;
43
43
import java .util .*;
44
+ import java .util .concurrent .ExecutorCompletionService ;
44
45
import java .util .concurrent .ExecutorService ;
45
- import java .util .concurrent .TimeUnit ;
46
46
47
47
import static com .graphhopper .util .Parameters .Landmark .DISABLE ;
48
48
@@ -264,6 +264,7 @@ public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) {
264
264
* @see com.graphhopper.routing.ch.CHAlgoFactoryDecorator#prepare(StorableProperties) for a very similar method
265
265
*/
266
266
public boolean loadOrDoWork (final StorableProperties properties ) {
267
+ ExecutorCompletionService completionService = new ExecutorCompletionService <>(threadPool );
267
268
int counter = 0 ;
268
269
boolean prepared = false ;
269
270
for (final PrepareLandmarks plm : preparations ) {
@@ -274,7 +275,7 @@ public boolean loadOrDoWork(final StorableProperties properties) {
274
275
prepared = true ;
275
276
LOGGER .info (counter + "/" + getPreparations ().size () + " calling LM prepare.doWork for " + plm .getWeighting () + " ... (" + Helper .getMemInfo () + ")" );
276
277
final String name = AbstractWeighting .weightingToFileName (plm .getWeighting ());
277
- threadPool . execute (new Runnable () {
278
+ completionService . submit (new Runnable () {
278
279
@ Override
279
280
public void run () {
280
281
String errorKey = Landmark .PREPARE + "error." + name ;
@@ -289,17 +290,17 @@ public void run() {
289
290
properties .put (errorKey , ex .getMessage ());
290
291
}
291
292
}
292
- });
293
+ }, name );
293
294
}
294
295
295
296
threadPool .shutdown ();
296
297
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 ) {
301
302
threadPool .shutdownNow ();
302
- throw new RuntimeException (ie );
303
+ throw new RuntimeException (e );
303
304
}
304
305
return prepared ;
305
306
}
0 commit comments