private boolean addWorker(Runnable firstTask,boolean core){
  for (; ; ) {
    int c=ctl.get();
    int rs=runStateOf(c);
    if (rs == SHUTDOWN) {
      if (workQueue.isEmpty())       return false;
      c=ctl.get();
    }
    int wc=workerCountOf(c);
    if (rs > SHUTDOWN || wc >= CAPACITY || wc >= (core ? corePoolSize : maximumPoolSize))     return false;
    if (ctl.compareAndSet(c,ctlOf(rs,wc + 1)))     break;
  }
  Worker w=new Worker(firstTask);
  Thread t=w.thread;
  if (t == null) {
    decrementWorkerCount();
    tryTerminate2();
    return false;
  }
  final ReentrantLock mainLock=this.mainLock;
  mainLock.lock();
  try {
    workers.add(w);
    int s=workers.size();
    if (s > largestPoolSize)     largestPoolSize=s;
  }
  finally {
    mainLock.unlock();
  }
  t.start();
  return true;
}

private boolean addWorker ( Runnable firstTask , boolean core ) { for ( ; ; ) { int c = ctl . get ( ) ; int rs = runStateOf ( c ) ; if ( rs == SHUTDOWN ) { if ( workQueue . isEmpty ( ) ) return false ; c = ctl . get ( ) ; } int wc = workerCountOf ( c ) ; if ( rs > SHUTDOWN || wc >= CAPACITY || wc >= ( core ? corePoolSize : maximumPoolSize ) ) return false ; if ( ctl . compareAndSet ( c , ctlOf ( rs , wc + 1 ) ) ) break ; } Worker w = new Worker ( firstTask ) ; Thread t = w . thread ; if ( t == null ) { decrementWorkerCount ( ) ; tryTerminate2 ( ) ; return false ; } final ReentrantLock mainLock = this . mainLock ; mainLock . lock ( ) ; try { workers . add ( w ) ; int s = workers . size ( ) ; if ( s > largestPoolSize ) largestPoolSize = s ; } finally { mainLock . unlock ( ) ; } t . start ( ) ; return true ; }
checks if a new worker can be added with respect to current pool state and the given bound ( either core or maximum ) . if so the worker count is adjusted accordingly , and , if possible , a new worker is created and started running firsttask as its first task , this method returns false if the pool is stopped or eligible to shut down . it also returns false if the thread factory fails to create a thread when asked , which requires a backout of workercount , and a recheck for termination , in case the existence of this worker was holding up termination .
