private boolean addWorker(Runnable firstTask,boolean core){
  retry:   for (; ; ) {
    int c=ctl.get();
    int rs=runStateOf(c);
    if (rs >= SHUTDOWN && !(rs == SHUTDOWN && firstTask == null && !workQueue.isEmpty()))     return false;
    for (; ; ) {
      int wc=workerCountOf(c);
      if (wc >= CAPACITY || wc >= (core ? corePoolSize : maximumPoolSize))       return false;
      if (compareAndIncrementWorkerCount(c))       break retry;
      c=ctl.get();
      if (runStateOf(c) != rs)       continue retry;
    }
  }
  boolean workerStarted=false;
  boolean workerAdded=false;
  Worker w=null;
  try {
    w=new Worker(firstTask);
    final Thread t=w.thread;
    if (t != null) {
      final ReentrantLock mainLock=this.mainLock;
      mainLock.lock();
      try {
        int rs=runStateOf(ctl.get());
        if (rs < SHUTDOWN || (rs == SHUTDOWN && firstTask == null)) {
          if (t.isAlive())           throw new IllegalThreadStateException();
          workers.add(w);
          int s=workers.size();
          if (s > largestPoolSize)           largestPoolSize=s;
          workerAdded=true;
        }
      }
  finally {
        mainLock.unlock();
      }
      if (workerAdded) {
        t.start();
        workerStarted=true;
      }
    }
  }
  finally {
    if (!workerStarted)     addWorkerFailed(w);
  }
  return workerStarted;
}

private boolean addWorker ( Runnable firstTask , boolean core ) { retry : for ( ; ; ) { int c = ctl . get ( ) ; int rs = runStateOf ( c ) ; if ( rs >= SHUTDOWN && ! ( rs == SHUTDOWN && firstTask == null && ! workQueue . isEmpty ( ) ) ) return false ; for ( ; ; ) { int wc = workerCountOf ( c ) ; if ( wc >= CAPACITY || wc >= ( core ? corePoolSize : maximumPoolSize ) ) return false ; if ( compareAndIncrementWorkerCount ( c ) ) break retry ; c = ctl . get ( ) ; if ( runStateOf ( c ) != rs ) continue retry ; } } boolean workerStarted = false ; boolean workerAdded = false ; Worker w = null ; try { w = new Worker ( firstTask ) ; final Thread t = w . thread ; if ( t != null ) { final ReentrantLock mainLock = this . mainLock ; mainLock . lock ( ) ; try { int rs = runStateOf ( ctl . get ( ) ) ; if ( rs < SHUTDOWN || ( rs == SHUTDOWN && firstTask == null ) ) { if ( t . isAlive ( ) ) throw new IllegalThreadStateException ( ) ; workers . add ( w ) ; int s = workers . size ( ) ; if ( s > largestPoolSize ) largestPoolSize = s ; workerAdded = true ; } } finally { mainLock . unlock ( ) ; } if ( workerAdded ) { t . start ( ) ; workerStarted = true ; } } } finally { if ( ! workerStarted ) addWorkerFailed ( w ) ; } return workerStarted ; }
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 . if the thread creation fails , either due to the thread factory returning null , or due to an exception ( typically outofmemoryerror in thread . start ( ) ) , we roll back cleanly .
