Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8258217

PriorityBlockingQueue constructor spec and behavior mismatch

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 17
    • 15
    • core-libs
    • None

    Description

      See the spec of PriorityBlockingQueue constructor, which accepts the Collection:
      https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/concurrent/PriorityBlockingQueue.html#%3Cinit%3E(java.util.Collection)

      public PriorityBlockingQueue​(Collection<? extends E> c)
      Creates a PriorityBlockingQueue containing the elements in the specified collection. If the specified collection is a SortedSet or a PriorityQueue, this priority queue will be ordered according to the same ordering. Otherwise, this priority queue will be ordered according to the natural ordering of its elements.

      The "or a PriorityQueue" part is wrong, which can be demonstrated with the following test:

      import java.util.Comparator;
      import java.util.PriorityQueue;
      import java.util.concurrent.PriorityBlockingQueue;

      public class PriorityBlockingQueueTest {
        public static void main(String[] args) {
          PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
          
          PriorityBlockingQueue<Integer> pbq = new PriorityBlockingQueue<>(pq);
          System.out.println(pbq.comparator()); // prints null
        }
      }

      So either the spec is wrong and it should tell 'PriorityBlockingQueue' instead of 'PriorityQueue', or the implementation is wrong.

      Attachments

        Issue Links

          Activity

            People

              martin Martin Buchholz
              tvaleev Tagir Valeev
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: