示例#1
0
 public boolean get() {
   while (!stopped) {
     long old = availables.getAndDecrement();
     if (old > 0) return true;
     try {
       synchronized (this) {
         this.wait();
       }
     } catch (InterruptedException e) {
       return false;
     }
   }
   return false;
 }
示例#2
0
  /**
   * Atomically subtracts from the size and count, if the queue is running in atomic counting mode,
   * or sizeable mode and the element is sizeable.
   *
   * @param record The record to update the size and count for.
   */
  private void decrementSizeAndCount(E record) {
    // Update the count for atomically counted queues.
    if (atomicallyCounted) {
      count.decrementAndGet();
    }

    // Update the size for sizeable elements and sizeable queues.
    if (sizeable && (record instanceof Sizeable)) {
      long recordSize = -((Sizeable) record).sizeof();
      long oldSize = dataSize.getAndAdd(recordSize);
      long newSize = oldSize + recordSize;

      signalOnSizeThresholdCrossing(oldSize, newSize);
    } else if (sizeable) {
      long oldSize = dataSize.getAndDecrement();
      long newSize = oldSize - 1;

      signalOnSizeThresholdCrossing(oldSize, newSize);
    }
  }
  @Override
  public void run() {
    long requests = concurrentRequests.getAndDecrement();

    if (deferredResult.isSetOrExpired()) {
      logger.warn(
          "{}: Processing of non-blocking request #{} already expired", requests, requestId);
    } else {
      logger.info("Retrieving result");
      NetworkServiceInstantiateReply reply = ServiceSelectionMsgTable.getInstance().get(requestId);
      if (reply != null) {
        logger.info("Retrieving reply from orchestrator with ns id {}", requestId);
      }

      boolean deferredStatus = deferredResult.setResult(reply);
      logger.debug(
          "{}: Processing of non-blocking request #{} done, deferredStatus = {}",
          requests,
          requestId,
          deferredStatus);
    }
  }
 @Override
 public T remove() {
   Long id = currentId.getAndIncrement();
   counter.getAndDecrement();
   return objects.remove(id);
 }
 /** getAndDecrement returns previous value and decrements */
 public void testGetAndDecrement() {
   AtomicLong ai = new AtomicLong(1);
   assertEquals(1, ai.getAndDecrement());
   assertEquals(0, ai.getAndDecrement());
   assertEquals(-1, ai.getAndDecrement());
 }