@Override protected void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); ManagedFutureTask task = (ManagedFutureTask) r; try { task.done(t); } finally { task.resetContext(); // Kill thread if thread older than threadLifeTime if (threadLifeTime > 0) { Thread thread = Thread.currentThread(); if (thread instanceof AbstractManagedThread) { long threadStartTime = ((AbstractManagedThread)thread).getThreadStartTime(); if ((System.currentTimeMillis() - threadStartTime)/1000 > threadLifeTime) { throw new ThreadExpiredException(); } } } } }