code: private boolean tryIncrement(AtomicInteger counter, int max) {
  
          // Repeatedly attempt to increment the given AtomicInteger until we
          // explicitly succeed or explicitly fail
          while (true) {
  
              // Get current value
              int count = counter.get();
  
              // Bail out if the maximum has already been reached
              if (count >= max && max != 0)
                  return false;
  
              // Attempt to increment
              if (counter.compareAndSet(count, count+1))
                  return true;
  
              // Try again if unsuccessful
  
          }
  
      }
docs: Attempts to increment the given AtomicInteger without exceeding the
  specified maximum value. If the AtomicInteger cannot be incremented
  without exceeding the maximum, false is returned.
  
  @param counter
  The AtomicInteger to attempt to increment.
  
  @param max
  The maximum value that the given AtomicInteger should contain, or
  zero if no limit applies.
  
  @return
  true if the AtomicInteger was successfully incremented without
  exceeding the specified maximum, false if the AtomicInteger could
  not be incremented.