I work on a simulation system, where at each timestep, I have to simulate many models. I used a FixedThreadPool to speed up the calculation:
ExecutorService executor = Executors.newFixedThreadPool(nThread);
for (Model m : models) {
executor.execute( m.simulationTask() );
}
executor.shutdown();
while ( ! executor.awaitTermination(10, TimeUnit.MINUTES) ) {
System.out.println("wait");
}
Now, the executor can't be used to execute()
new Tasks after calling shutdown()
. Is there a way to reset the executor, so I can reuse the existing executor (and its threads) at the next simulation step?
You can reuse the executor service if you restructure your code somewhat.
Basically you collect all your tasks, execute them, and await execution before proceeding. Of course, you could also alternatively just use a new Executor Service for each of your time steps, but at least you have options.
Caveats: I didn't compile the code so there might be errors. I also assumed an Integer parameter type for convenience.