@Override
  public int drainTo(Collection<? super E> c, int maxElements) {
    Objects.requireNonNull(c);
    if (c == this) throw new IllegalArgumentException();
    if (maxElements <= 0) return 0;
    lock.lock();
    try {
      int n = Math.min(queue.size(), maxElements);
      for (int i = 0; i < n; ++i) {
        c.add(queue.poll());
      }
      return n;
    } finally {
      lock.unlock();
    }
  }
Creates a PriorityQueue with the specified capacity that orders its elements according to the
specified comparator.
@param capacity   the capacity of this queue
@param comparator the comparator that will be used to order this priority queue
hbase-server/src/main/java/org/apache/hadoop/hbase/util/BoundedPriorityBlockingQueue.java