private boolean putSessionBack(SessionType session, boolean isFirst) {
    SettableFuture<SessionType> future = null;
    poolLock.lock();
    try {
      // See if we need to kill some sessions because the pool was resized down while
      // a bunch of sessions were outstanding. See also deltaRemaining javadoc.
      while (true) {
        int remainingToKill = -deltaRemaining.get();
        if (remainingToKill <= 0) break; // No need to kill anything.
        if (deltaRemaining.compareAndSet(-remainingToKill, -remainingToKill + 1)) {
          return false;
        }
      }
      // If there are async requests, satisfy them first.
      if (!asyncRequests.isEmpty()) {
        if (!session.tryUse(false)) {
          return true; // Session has expired and will be returned to us later.
        }
        future = asyncRequests.poll();
      }
      if (future == null) {
        // Put session into the pool.
        if (isFirst) {
          pool.addFirst(session);
        } else {
          pool.addLast(session);
        }
        notEmpty.signalAll();
      }
    } finally {
      poolLock.unlock();
    }
    if (future != null) {
      future.set(session);
    }
    return true;
  }
Puts session back into the pool.
@return true if the session has been put back; false if it's not needed and should be killed.
ql/src/java/org/apache/hadoop/hive/ql/exec/tez/TezSessionPool.java