So I asked a question a little while ago: Over here that asked the question “How can I make my threads get killed if they take too long”
I have implemented the solution mentioned there but under certain rare circumstances where a thread times out, the program can still fail / lock up (see: Keep the main() method open, and prevent further cron runs of the program).
Here’s the source I’m using:
//Iterate through the array to submit them into individual running threads.
ExecutorService threadPool = Executors.newFixedThreadPool(12);
List<Future<?>> taskList = new ArrayList<Future<?>>();
for (int i = 0; i < objectArray.length; i++) {
Future<?> task = threadPool.submit(new ThreadHandler(objectArray[i], i));
taskList.add(task);
Thread.sleep(500);
}
//Event handler to kill any threads that are running for more than 30 seconds (most threads should only need .25 - 1 second to complete.
for(Future future : taskList){
try{
future.get(30, TimeUnit.SECONDS);
}catch(CancellationException cx){ System.err.println("Cancellation Exception: "); cx.printStackTrace();
}catch(ExecutionException ex){ System.err.println("Execution Exception: ");ex.printStackTrace();
}catch(InterruptedException ix){ System.err.println("Interrupted Exception: ");ix.printStackTrace();
}catch(TimeoutException ex) {future.cancel(true);}
}
threadPool.shutdown();
threadPool.awaitTermination(60, TimeUnit.SECONDS);
So my question is: with this code implemented, why is the executor service not cutting things off at 30 seconds.
Because I suspect your worker threads are still running. You are calling
future.cancel(true);but all that does is set the interrupt flag on the thread — it does not actively interrupt your running code. Another way to “interrupt” code is to set somevolatile boolean shutdownflag to be true and to test for that in your code. See here for more details about interrupting threads.You need to make sure that your
ThreadHandlercode handles interrupts correctly. It needs to, for example, checkThread.currentThread().isInterrupted()in loops or in other code blocks. You also need to make sure that you are handlingInterruptedExceptioncorrectly and not just swallowing the interrupt.See my answer here for more information about thread interrupts.