Skip to content

Commit 83d27b2

Browse files
committed
Share mapping executor across reactions; name threads for profiling
Replace per-call newFixedThreadPool with a single static pool (2–3 threads, daemon). Avoids repeated thread creation/teardown overhead in batch processing. Each call still gets its own CompletionService so concurrent invocations cannot observe each other's results.
1 parent 1af8c2c commit 83d27b2

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

src/main/java/com/bioinceptionlabs/reactionblast/mapping/CallableAtomMappingTool.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import java.util.concurrent.ExecutorCompletionService;
3131
import java.util.concurrent.ExecutorService;
3232
import java.util.concurrent.Executors;
33-
import java.util.concurrent.TimeUnit;
33+
import java.util.concurrent.ThreadFactory;
3434

3535
import org.openscience.cdk.graph.Cycles;
3636
import 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

Comments
 (0)