3030import java .util .concurrent .ExecutorCompletionService ;
3131import java .util .concurrent .ExecutorService ;
3232import java .util .concurrent .Executors ;
33- import java .util .concurrent .TimeUnit ;
33+ import java .util .concurrent .ThreadFactory ;
3434
3535import org .openscience .cdk .graph .Cycles ;
3636import org .openscience .cdk .interfaces .IAtom ;
@@ -59,6 +59,10 @@ public class CallableAtomMappingTool implements Serializable {
5959 private final static ILoggingTool LOGGER
6060 = createLoggingTool (CallableAtomMappingTool .class );
6161 private static final long serialVersionUID = 0x29e2adb1716b13eL ;
62+ private static final int MAPPING_PARALLELISM
63+ = Math .max (2 , Math .min (3 , Runtime .getRuntime ().availableProcessors ()));
64+ private static final ExecutorService MAPPING_EXECUTOR
65+ = Executors .newFixedThreadPool (MAPPING_PARALLELISM , new MappingThreadFactory ());
6266
6367 private Map <IMappingAlgorithm , Reactor > solution = null ;
6468
@@ -180,9 +184,8 @@ private void generateAtomAtomMapping(
180184 remaining = new IMappingAlgorithm []{MIN , MAX , RINGS };
181185 }
182186
183- ExecutorService executor = Executors .newFixedThreadPool (remaining .length );
184187 try {
185- CompletionService <Reactor > cs = new ExecutorCompletionService <>(executor );
188+ CompletionService <Reactor > cs = new ExecutorCompletionService <>(MAPPING_EXECUTOR );
186189 int jobCounter = 0 ;
187190 for (IMappingAlgorithm algo : remaining ) {
188191 LOGGER .debug ("Submitting " + algo .description ());
@@ -194,8 +197,6 @@ private void generateAtomAtomMapping(
194197 Reactor chosen = cs .take ().get ();
195198 putSolution (chosen .getAlgorithm (), chosen );
196199 }
197- executor .shutdown ();
198- executor .awaitTermination (Long .MAX_VALUE , TimeUnit .NANOSECONDS );
199200 LOGGER .debug ("======DONE CallableAtomMappingTool=======" );
200201 } catch (InterruptedException | ExecutionException e ) {
201202 LOGGER .debug ("ERROR: in AtomMappingTool: " + e .getMessage ());
@@ -206,7 +207,6 @@ private void generateAtomAtomMapping(
206207 standardizedReaction .getID (),
207208 currentTimeMillis () - mappingStart );
208209 }
209- executor .shutdown ();
210210 LOGGER .debug ("!!!!Atom-Atom Mapping Done!!!!" );
211211 ThreadSafeCache .getInstance ().cleanup ();
212212 }
@@ -401,4 +401,14 @@ public Reactor call() throws Exception {
401401 }
402402 }
403403
404+ private static final class MappingThreadFactory implements ThreadFactory {
405+
406+ @ Override
407+ public Thread newThread (Runnable runnable ) {
408+ Thread thread = new Thread (runnable , "rdt-mapping" );
409+ thread .setDaemon (true );
410+ return thread ;
411+ }
412+ }
413+
404414}
0 commit comments