Java Code Examples for java.util.concurrent.ArrayBlockingQueue#remainingCapacity()

The following examples show how to use java.util.concurrent.ArrayBlockingQueue#remainingCapacity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChannelDataFlowOperation.java    From twister2 with Apache License 2.0 6 votes vote down vote up
/**
 * Put the message into internal queues, to be serialized and then send to the network channel
 *
 * @param source source
 * @param message data
 * @param target target
 * @param flags flags
 * @param routingParameters routing parameters
 * @param pendingSendMessages the message queue
 * @return true if message is accepted
 */
private boolean offerForSend(int source, Object message, int target, int flags,
                             RoutingParameters routingParameters,
                             ArrayBlockingQueue<OutMessage> pendingSendMessages) {
  if (pendingSendMessages.remainingCapacity() > 0) {
    int path = DEFAULT_PATH;
    if (routingParameters.getExternalRoutes().size() > 0) {
      path = routingParameters.getDestinationId();
    }

    OutMessage sendMessage = new OutMessage(source, edge,
        path, target, flags, routingParameters.getInternalRoutes(),
        routingParameters.getExternalRoutes(), dataType, keyType, this, message);

    // now try to put this into pending
    return pendingSendMessages.offer(sendMessage);
  }
  return false;
}
 
Example 2
Source File: ControlledChannelOperation.java    From twister2 with Apache License 2.0 6 votes vote down vote up
/**
 * Put the message into internal queues, to be serialized and then send to the network channel
 *
 * @param source source
 * @param message data
 * @param target target
 * @param flags flags
 * @param routingParameters routing parameters
 * @param pendingSendMessages the message queue
 * @return true if message is accepted
 */
private boolean offerForSend(int source, Object message, int target, int flags,
                             RoutingParameters routingParameters,
                             ArrayBlockingQueue<OutMessage> pendingSendMessages) {
  if (pendingSendMessages.remainingCapacity() > 0) {
    int path = DEFAULT_PATH;
    if (routingParameters.getExternalRoutes().size() > 0) {
      path = routingParameters.getDestinationId();
    }

    OutMessage sendMessage = new OutMessage(source, edge,
        path, target, flags, routingParameters.getInternalRoutes(),
        routingParameters.getExternalRoutes(), dataType, keyType, this, message);

    // now try to put this into pending
    return pendingSendMessages.offer(sendMessage);
  }
  return false;
}
 
Example 3
Source File: DefaultGroovyMethodsSupport.java    From groovy with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected static <T> Queue<T> createSimilarQueue(Queue<T> orig) {
    if (orig instanceof ArrayBlockingQueue) {
        ArrayBlockingQueue queue = (ArrayBlockingQueue) orig;
        return new ArrayBlockingQueue<T>(queue.size() + queue.remainingCapacity());
    } else if (orig instanceof ArrayDeque) {
        return new ArrayDeque<T>();
    } else if (orig instanceof ConcurrentLinkedQueue) {
        return new ConcurrentLinkedQueue<T>();
    } else if (orig instanceof DelayQueue) {
        return new DelayQueue();
    } else if (orig instanceof LinkedBlockingDeque) {
        return new LinkedBlockingDeque<T>();
    } else if (orig instanceof LinkedBlockingQueue) {
        return new LinkedBlockingQueue<T>();
    } else if (orig instanceof PriorityBlockingQueue) {
        return new PriorityBlockingQueue<T>();
    } else if (orig instanceof PriorityQueue) {
        return new PriorityQueue<T>(11, ((PriorityQueue) orig).comparator());
    } else if (orig instanceof SynchronousQueue) {
        return new SynchronousQueue<T>();
    } else {
        return new LinkedList<T>();
    }
}
 
Example 4
Source File: ArrayBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * clear removes all elements
 */
public void testClear() {
    int size = ThreadLocalRandom.current().nextInt(1, 5);
    ArrayBlockingQueue q = populatedQueue(size, size, 2 * size, false);
    int capacity = size + q.remainingCapacity();
    q.clear();
    assertTrue(q.isEmpty());
    assertEquals(0, q.size());
    assertEquals(capacity, q.remainingCapacity());
    q.add(one);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(one));
    q.clear();
    assertTrue(q.isEmpty());
}
 
Example 5
Source File: WhiteBox.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Instead of having putIndex (and takeIndex) at the initial
 * default of 0, move them to a random location.
 */
void randomizePutIndex(ArrayBlockingQueue q) {
    assertTrue(q.isEmpty());
    int capacity = q.remainingCapacity();
    int n = rnd.nextInt(capacity + 1);
    int putIndex = putIndex(q);
    for (int i = n; i-->0; ) q.add(Boolean.TRUE);
    for (int i = n; i-->0; ) q.remove();
    assertEquals(putIndex(q), (putIndex + n) % items(q).length);
}