Tabnine Logo
Queue
Code IndexAdd Tabnine to your IDE (free)

How to use
Queue
in
java.util

Best Java code snippets using java.util.Queue (Showing top 20 results out of 30,870)

origin: google/guava

/** Enqueues a event to be run. */
synchronized void add(ListenerCallQueue.Event<L> event, Object label) {
 waitQueue.add(event);
 labelQueue.add(label);
}
origin: netty/netty

protected static Runnable pollTaskFrom(Queue<Runnable> taskQueue) {
  for (;;) {
    Runnable task = taskQueue.poll();
    if (task == WAKEUP_TASK) {
      continue;
    }
    return task;
  }
}
origin: google/guava

@Override
public boolean hasNext() {
 return !queue.isEmpty();
}
origin: bumptech/glide

public void offer(T key) {
 if (keyPool.size() < MAX_SIZE) {
  keyPool.offer(key);
 }
}
origin: bumptech/glide

static void clearQueue() {
 while (!QUEUE.isEmpty()) {
  QUEUE.remove();
 }
}
origin: ReactiveX/RxJava

private void triggerActions(long targetTimeInNanoseconds) {
  for (;;) {
    TimedRunnable current = queue.peek();
    if (current == null || current.time > targetTimeInNanoseconds) {
      break;
    }
    // if scheduled time is 0 (immediate) use current virtual time
    time = current.time == 0 ? time : current.time;
    queue.remove(current);
    // Only execute if not unsubscribed
    if (!current.scheduler.disposed) {
      current.run.run();
    }
  }
  time = targetTimeInNanoseconds;
}
origin: google/guava

 public void testHoldsLockOnAllOperations() {
  create().element();
  create().offer("foo");
  create().peek();
  create().poll();
  create().remove();
  create().add("foo");
  create().addAll(ImmutableList.of("foo"));
  create().clear();
  create().contains("foo");
  create().containsAll(ImmutableList.of("foo"));
  create().equals(new ArrayDeque<>(ImmutableList.of("foo")));
  create().hashCode();
  create().isEmpty();
  create().iterator();
  create().remove("foo");
  create().removeAll(ImmutableList.of("foo"));
  create().retainAll(ImmutableList.of("foo"));
  create().size();
  create().toArray();
  create().toArray(new String[] {"foo"});
 }
}
origin: org.mockito/mockito-core

public Object answer(InvocationOnMock invocation) throws Throwable {
  //see ThreadsShareGenerouslyStubbedMockTest
  Answer a;
  synchronized(answers) {
    a = answers.size() == 1 ? answers.peek() : answers.poll();
  }
  return a.answer(invocation);
}
origin: google/guava

 @Override
 public T next() {
  PeekingIterator<T> nextIter = queue.remove();
  T next = nextIter.next();
  if (nextIter.hasNext()) {
   queue.add(nextIter);
  }
  return next;
 }
}
origin: ReactiveX/RxJava

  @Override
  public void run() {
    queue.remove(timedAction);
  }
}
origin: spring-projects/spring-framework

private ByteBuffer assembleChunksAndReset() {
  ByteBuffer result;
  if (this.chunks.size() == 1) {
    result = this.chunks.remove();
  }
  else {
    result = ByteBuffer.allocate(getBufferSize());
    for (ByteBuffer partial : this.chunks) {
      result.put(partial);
    }
    result.flip();
  }
  this.chunks.clear();
  this.expectedContentLength = null;
  return result;
}
origin: spring-projects/spring-framework

@Override
protected void flushCache() throws SockJsTransportFailureException {
  String[] messages = new String[getMessageCache().size()];
  for (int i = 0; i < messages.length; i++) {
    messages[i] = getMessageCache().poll();
  }
  SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec();
  SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, messages);
  writeFrame(frame);
}
origin: google/guava

public void testConsumingIterable_queue_removesFromQueue() {
 Queue<Integer> queue = Lists.newLinkedList(asList(5, 14));
 Iterator<Integer> consumingIterator = Iterables.consumingIterable(queue).iterator();
 assertEquals(5, queue.peek().intValue());
 assertEquals(5, consumingIterator.next().intValue());
 assertEquals(14, queue.peek().intValue());
 assertTrue(consumingIterator.hasNext());
 assertTrue(queue.isEmpty());
}
origin: netty/netty

/**
 * Return the number of tasks that are pending for processing.
 *
 * <strong>Be aware that this operation may be expensive as it depends on the internal implementation of the
 * SingleThreadEventExecutor. So use it with care!</strong>
 */
public int pendingTasks() {
  return taskQueue.size();
}
origin: ReactiveX/RxJava

  @Override
  public void accept(GroupedUnicast<K, V> value) {
    evictedGroups.offer(value);
  }
}
origin: netty/netty

final ScheduledFutureTask<?> peekScheduledTask() {
  Queue<ScheduledFutureTask<?>> scheduledTaskQueue = this.scheduledTaskQueue;
  if (scheduledTaskQueue == null) {
    return null;
  }
  return scheduledTaskQueue.peek();
}
origin: prestodb/presto

public synchronized List<PrioritizedSplitRunner> destroy()
{
  destroyed = true;
  ImmutableList.Builder<PrioritizedSplitRunner> builder = ImmutableList.builder();
  builder.addAll(runningIntermediateSplits);
  builder.addAll(runningLeafSplits);
  builder.addAll(queuedLeafSplits);
  runningIntermediateSplits.clear();
  runningLeafSplits.clear();
  queuedLeafSplits.clear();
  return builder.build();
}
origin: google/guava

@Override
public E remove() {
 synchronized (mutex) {
  return delegate().remove();
 }
}
origin: google/guava

 @Override
 public T computeNext() {
  return queue.isEmpty() ? endOfData() : queue.remove();
 }
}
origin: google/guava

/** Lookups on the map view shouldn't impact the recency queue. */
public void testAsMapRecency() {
 CacheBuilder<Object, Object> builder =
   createCacheBuilder().concurrencyLevel(1).maximumSize(SMALL_MAX_SIZE);
 LocalLoadingCache<Object, Object> cache = makeCache(builder, identityLoader());
 Segment<Object, Object> segment = cache.localCache.segments[0];
 ConcurrentMap<Object, Object> map = cache.asMap();
 Object one = new Object();
 assertSame(one, cache.getUnchecked(one));
 assertTrue(segment.recencyQueue.isEmpty());
 assertSame(one, map.get(one));
 assertSame(one, segment.recencyQueue.peek().getKey());
 assertSame(one, cache.getUnchecked(one));
 assertFalse(segment.recencyQueue.isEmpty());
}
java.utilQueue

Javadoc

A collection designed for holding elements prior to processing. Besides basic java.util.Collection operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restricted Queue implementations; in most implementations, insert operations cannot fail.

Throws exception Returns special value
Insert #add #offer
Remove #remove #poll
Examine #element #peek

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out). Whatever the ordering used, the head of the queue is that element which would be removed by a call to #remove() or #poll(). In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.

The #offer method inserts an element if possible, otherwise returning false. This differs from the java.util.Collection#add method, which can fail to add an element only by throwing an unchecked exception. The offer method is designed for use when failure is a normal, rather than exceptional occurrence, for example, in fixed-capacity (or "bounded") queues.

The #remove() and #poll() methods remove and return the head of the queue. Exactly which element is removed from the queue is a function of the queue's ordering policy, which differs from implementation to implementation. The remove() and poll() methods differ only in their behavior when the queue is empty: the remove() method throws an exception, while the poll() method returns null.

The #element() and #peek() methods return, but do not remove, the head of the queue.

The Queue interface does not define the blocking queue methods, which are common in concurrent programming. These methods, which wait for elements to appear or for space to become available, are defined in the java.util.concurrent.BlockingQueue interface, which extends this interface.

Queue implementations generally do not allow insertion of null elements, although some implementations, such as LinkedList, do not prohibit insertion of null. Even in the implementations that permit it, null should not be inserted into a Queue, as null is also used as a special return value by the poll method to indicate that the queue contains no elements.

Queue implementations generally do not define element-based versions of methods equals and hashCode but instead inherit the identity based versions from class Object, because element-based equality is not always well-defined for queues with the same elements but different ordering properties.

Most used methods

  • add
    Inserts the specified element into this queue if it is possible to do so immediately without violati
  • poll
    Retrieves and removes the head of this queue, or returns null if this queue is empty.
  • isEmpty
  • size
  • remove
  • offer
    Inserts the specified element into this queue if it is possible to do so immediately without violati
  • clear
  • peek
    Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
  • addAll
  • iterator
  • contains
  • element
    Retrieves, but does not remove, the head of this queue. This method differs from #peek only in that
  • contains,
  • element,
  • toArray,
  • stream,
  • forEach,
  • removeAll,
  • retainAll,
  • containsAll,
  • removeIf,
  • parallelStream

Popular in Java

  • Making http requests using okhttp
  • requestLocationUpdates (LocationManager)
  • compareTo (BigDecimal)
  • setContentView (Activity)
  • GridBagLayout (java.awt)
    The GridBagLayout class is a flexible layout manager that aligns components vertically and horizonta
  • MalformedURLException (java.net)
    This exception is thrown when a program attempts to create an URL from an incorrect specification.
  • Time (java.sql)
    Java representation of an SQL TIME value. Provides utilities to format and parse the time's represen
  • JTextField (javax.swing)
  • Base64 (org.apache.commons.codec.binary)
    Provides Base64 encoding and decoding as defined by RFC 2045.This class implements section 6.8. Base
  • Scheduler (org.quartz)
    This is the main interface of a Quartz Scheduler. A Scheduler maintains a registry of org.quartz.Job
  • Github Copilot alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now