21import java.util.ArrayList;
23import java.util.concurrent.CompletionService;
24import java.util.concurrent.ExecutionException;
25import java.util.concurrent.ExecutorCompletionService;
26import java.util.concurrent.ExecutorService;
27import java.util.concurrent.Executors;
28import java.util.concurrent.Future;
29import java.util.concurrent.TimeUnit;
31import denoptim.exception.DENOPTIMException;
32import denoptim.graph.Candidate;
67 int numOfJobs = syncronisedTasks.size();
69 int n = Math.min(numOfJobs, numOfProcessors);
76 eservice = Executors.newFixedThreadPool(n);
77 CompletionService<Object> cservice =
78 new ExecutorCompletionService<>(
eservice);
81 for (
int i=0; i<numOfJobs; i++)
83 futures.add(cservice.submit(syncronisedTasks.get(i)));
86 Thread shutDownHook =
new Thread()
95 if (!
eservice.awaitTermination(30, TimeUnit.SECONDS))
100 if (!
eservice.awaitTermination(60, TimeUnit.SECONDS))
105 catch (InterruptedException ie)
107 for (
Task tsk : syncronisedTasks)
112 syncronisedTasks.clear();
114 for (Future<Object> f :
futures)
122 Thread.currentThread().interrupt();
126 Runtime.getRuntime().addShutdownHook(shutDownHook);
129 ArrayList<Candidate> results =
new ArrayList<>();
132 for (
int i=0; i<syncronisedTasks.size(); i++)
135 results.add(taskResult);
138 catch (InterruptedException ie)
143 Thread.currentThread().interrupt();
146 catch (ExecutionException ee)
153 Runtime.getRuntime().removeShutdownHook(shutDownHook);
158 syncronisedTasks.clear();
159 for (Future<Object> f :
futures)
179 for (Future<Object> f :
futures)
185 if (!
eservice.awaitTermination(5, TimeUnit.SECONDS))
190 catch (InterruptedException ie)
A candidate is the combination of a denoptim graph with molecular representation and may include also...
A task that can throw exceptions.
Class that manages the submission of a batch of tasks.
List< Candidate > executeTasks(List< Task > syncronisedTasks, int numOfProcessors)
Execute the list of tasks.
List< Future< Object > > futures
TasksBatchManager()
Constructs a new task manager meant to run tasks in batches.