Refine search
/** * Creates an empty {@code PriorityQueue} with the ordering given by its elements' natural * ordering. * * @since 11.0 (requires that {@code E} be {@code Comparable} since 15.0). */ public static <E extends Comparable> PriorityQueue<E> newPriorityQueue() { return new PriorityQueue<E>(); }
/** * 添加一个元素 * @param e 元素 * @return 是否添加成功 */ public boolean add(E e) { if (queue.size() < maxSize) { // 未达到最大容量,直接添加 queue.add(e); return true; } else { // 队列已满 E peek = queue.peek(); if (queue.comparator().compare(e, peek) > 0) { // 将新元素与当前堆顶元素比较,保留较小的元素 queue.poll(); queue.add(e); return true; } } return false; }
lock.lockInterruptibly(); try { for (;;) { E first = q.peek(); if (first == null) available.await(); else { long delay = first.getDelay(NANOSECONDS); if (delay <= 0) return q.poll(); available.await(); else { Thread thisThread = Thread.currentThread(); leader = thisThread; try { available.awaitNanos(delay); } finally { if (leader == thisThread) if (leader == null && q.peek() != null) available.signal(); lock.unlock();
/** * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ public int drainTo(Collection<? super E> c) { if (c == null) throw new NullPointerException(); if (c == this) throw new IllegalArgumentException(); final ReentrantLock lock = this.lock; lock.lock(); try { int n = 0; for (E e; (e = peekExpired()) != null;) { c.add(e); // In this order, in case add() throws. q.poll(); ++n; } return n; } finally { lock.unlock(); } }
/** * Retrieves and removes the head of this queue, or returns {@code null} * if this queue has no elements with an expired delay. * * @return the head of this queue, or {@code null} if this * queue has no elements with an expired delay */ public E poll() { final ReentrantLock lock = this.lock; lock.lock(); try { E first = q.peek(); if (first == null || first.getDelay(NANOSECONDS) > 0) return null; else return q.poll(); } finally { lock.unlock(); } }
/** * Inserts the specified element into this delay queue. * * @param e the element to add * @return {@code true} * @throws NullPointerException if the specified element is null */ public boolean offer(E e) { final ReentrantLock lock = this.lock; lock.lock(); try { q.offer(e); if (q.peek() == e) { leader = null; available.signal(); } return true; } finally { lock.unlock(); } }
/** * Saves this queue to a stream (that is, serializes it). * * For compatibility with previous version of this class, elements * are first copied to a java.util.PriorityQueue, which is then * serialized. */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { lock.lock(); try { // avoid zero capacity argument q = new PriorityQueue<E>(Math.max(size, 1), comparator); q.addAll(this); s.defaultWriteObject(); } finally { q = null; lock.unlock(); } }
if(!evictionLock.tryLock()) return; PriorityQueue<BlockBucket> bucketQueue = new PriorityQueue<>(3); bucketQueue.add(bucketSingle); bucketQueue.add(bucketMulti); bucketQueue.add(bucketMemory); while ((bucket = bucketQueue.poll()) != null) { long overflow = bucket.overflow(); if (overflow > 0) { stats.evict(); evictionInProgress = false; evictionLock.unlock();
/** * Retrieves, but does not remove, the head of this queue, or * returns {@code null} if this queue is empty. Unlike * {@code poll}, if no expired elements are available in the queue, * this method returns the element that will expire next, * if one exists. * * @return the head of this queue, or {@code null} if this * queue is empty */ public E peek() { final ReentrantLock lock = this.lock; lock.lock(); try { return q.peek(); } finally { lock.unlock(); } }
private void testCustomAggregation(Double[] values, int n) { PriorityQueue<Double> heap = new PriorityQueue<>(n, (x, y) -> -Double.compare(x, y)); Arrays.stream(values).filter(x -> x != null).forEach(heap::add); Double[] expected = new Double[heap.size()]; for (int i = heap.size() - 1; i >= 0; i--) { expected[i] = heap.remove(); } testAggregation(Arrays.asList(expected), createDoublesBlock(values), createLongRepeatBlock(n, values.length)); } }
@Override public void nextCallback(T value) { values.add(value); }
@Override public TopNResultBuilder addEntry(DimensionAndMetricValueExtractor dimensionAndMetricValueExtractor) { Object dimensionValueObj = dimensionAndMetricValueExtractor.getDimensionValue(dimSpec.getOutputName()); String dimensionValue = Objects.toString(dimensionValueObj, null); if (shouldAdd(dimensionValue)) { pQueue.add( new DimValHolder.Builder().withDimValue(dimensionValue) .withMetricValues(dimensionAndMetricValueExtractor.getBaseObject()) .build() ); if (pQueue.size() > threshold) { pQueue.poll(); } } return this; }
private static int[][] countCounts2IntArrays(Counter<Integer> countCounts) { int size = countCounts.size(); int[][] arrays = new int[2][]; arrays[0] = new int[size]; // counts arrays[1] = new int[size]; // count counts PriorityQueue<Integer> q = new PriorityQueue<>(countCounts.keySet()); int i = 0; while (!q.isEmpty()) { Integer count = q.poll(); Integer countCount = (int) Math.round(countCounts.getCount(count)); arrays[0][i] = count; arrays[1][i] = countCount; i++; } return arrays; }