@Nullable @CheckReturnValue private List<Ticket> tryAcquireAtomically(int tickets) throws NoCapacityAvailableException {
  List<Ticket> acquiredParentTickets=new ArrayList<>();
  mLock.lock();
  try {
    if (tickets > mCapacity) {
      throw new NoCapacityAvailableException();
    }
    if (mParentTickets.size() >= tickets) {
      for (int i=0; i < tickets; i++) {
        acquiredParentTickets.add(mParentTickets.remove());
      }
      updateCurrentTicketCount();
      return acquiredParentTickets;
    }
  }
  finally {
    mLock.unlock();
  }
  return null;
}

@ Nullable @ CheckReturnValue private List < Ticket > tryAcquireAtomically ( int tickets ) throws NoCapacityAvailableException { List < Ticket > acquiredParentTickets = new ArrayList < > ( ) ; mLock . lock ( ) ; try { if ( tickets > mCapacity ) { throw new NoCapacityAvailableException ( ) ; } if ( mParentTickets . size ( ) >= tickets ) { for ( int i = 0 ; i < tickets ; i ++ ) { acquiredParentTickets . add ( mParentTickets . remove ( ) ) ; } updateCurrentTicketCount ( ) ; return acquiredParentTickets ; } } finally { mLock . unlock ( ) ; } return null ; }
atomically attempt to remove the necessary number of tickets . this must be an all - or - nothing attempt to avoid multiple acquire ( ) calls from deadlocking by each partially acquiring the necessary number of tickets .
