private voidrunTask(Runnable task) {final ReentrantLock runLock = this.runLock;
runLock.lock();try{/** If pool is stopping ensure thread is interrupted;
* if not, ensure thread is not interrupted. This requires
* a double-check of state in case the interrupt was
* cleared concurrently with a shutdownNow -- if so,
* the interrupt is re-enabled.*/
if ((runState >= STOP ||(Thread.interrupted()&& runState >= STOP)) &&hasRun)
thread.interrupt();/** Track execution state to ensure that afterExecute
* is called only if task completed or threw
* exception. Otherwise, the caught runtime exception
* will have been thrown by afterExecute itself, in
* which case we don't want to call it again.*/
boolean ran = false;
beforeExecute(thread, task);try{
task.run();
ran= true;
afterExecute(task,null);++completedTasks;
}catch(RuntimeException ex) {if (!ran)
afterExecute(task, ex);throwex;
}
}finally{
runLock.unlock();
}
}