final void runWorker(Worker w){
  Thread wt=Thread.currentThread();
  Runnable task=w.firstTask;
  w.firstTask=null;
  w.unlock();
  boolean completedAbruptly=true;
  try {
    while (task != null || (task=getTask()) != null) {
      w.lock();
      if ((runStateAtLeast(ctl.get(),STOP) || (Thread.interrupted() && runStateAtLeast(ctl.get(),STOP))) && !wt.isInterrupted())       wt.interrupt();
      try {
        beforeExecute(wt,task);
        Throwable thrown=null;
        try {
          task.run();
        }
 catch (        RuntimeException x) {
          thrown=x;
          throw x;
        }
catch (        Error x) {
          thrown=x;
          throw x;
        }
catch (        Throwable x) {
          thrown=x;
          throw new Error(x);
        }
 finally {
          afterExecute(task,thrown);
        }
      }
  finally {
        task=null;
        w.completedTasks++;
        w.unlock();
      }
    }
    completedAbruptly=false;
  }
  finally {
    processWorkerExit(w,completedAbruptly);
  }
}

final void runWorker ( Worker w ) { Thread wt = Thread . currentThread ( ) ; Runnable task = w . firstTask ; w . firstTask = null ; w . unlock ( ) ; boolean completedAbruptly = true ; try { while ( task != null || ( task = getTask ( ) ) != null ) { w . lock ( ) ; if ( ( runStateAtLeast ( ctl . get ( ) , STOP ) || ( Thread . interrupted ( ) && runStateAtLeast ( ctl . get ( ) , STOP ) ) ) && ! wt . isInterrupted ( ) ) wt . interrupt ( ) ; try { beforeExecute ( wt , task ) ; Throwable thrown = null ; try { task . run ( ) ; } catch ( RuntimeException x ) { thrown = x ; throw x ; } catch ( Error x ) { thrown = x ; throw x ; } catch ( Throwable x ) { thrown = x ; throw new Error ( x ) ; } finally { afterExecute ( task , thrown ) ; } } finally { task = null ; w . completedTasks ++ ; w . unlock ( ) ; } } completedAbruptly = false ; } finally { processWorkerExit ( w , completedAbruptly ) ; } }
main worker run loop . repeatedly gets tasks from queue and executes them , while coping with a number of issues : 1 . we may start out with an initial task , in which case we don ' t need to get the first one . otherwise , as long as pool is running , we get tasks from gettask . if it returns null then the worker exits due to changed pool state or configuration parameters . other exits result from exception throws in external code , in which case completedabruptly holds , which usually leads processworkerexit to replace this thread . 2 . before running any task , the lock is acquired to prevent other pool interrupts while the task is executing , and then we ensure that unless pool is stopping , this thread does not have its interrupt set . 3 . each task run is preceded by a call to beforeexecute , which might throw an exception , in which case we cause thread to die ( breaking loop with completedabruptly true ) without processing the task . 4 . assuming beforeexecute completes normally , we run the task , gathering any of its thrown exceptions to send to afterexecute . we separately handle runtimeexception , error ( both of which the specs guarantee that we trap ) and arbitrary throwables . because we cannot rethrow throwables within runnable . run , we wrap them within errors on the way out ( to the thread ' s uncaughtexceptionhandler ) . any thrown exception also conservatively causes thread to die . 5 . after task . run completes , we call afterexecute , which may also throw an exception , which will also cause thread to die . according to jls sec 14 . 20 , this exception is the one that will be in effect even if task . run throws . the net effect of the exception mechanics is that afterexecute and the thread ' s uncaughtexceptionhandler have as accurate information as we can provide about any problems encountered by user code .
