java - 为什么PriorityBlockingQueue队列没有​​按照优先级对元素进行排序

标签 java concurrency blockingqueue blockingcollection

这是我的代码,运行结束的代码不是我的异常(exception)。

我认为PriorityBlockingQueue是按照Priority排序的,但是结局却不是我所期望的,谁能告诉我为什么。

 public class TestPriorityQueue {  

    static Random r=new Random(47);  

    public static void main(String args[]) throws InterruptedException{  
        final PriorityBlockingQueue q=new PriorityBlockingQueue();  
        ExecutorService se=Executors.newCachedThreadPool();  
        //execute producer  
        se.execute(new Runnable(){  
            public void run() {  
                int i=0;  
                while(true){  
                    q.put(new PriorityEntity(r.nextInt(10),i++));  
                    try {  
                        TimeUnit.MILLISECONDS.sleep(r.nextInt(1000));  
                    } catch (InterruptedException e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                }  
            }  
        }); 

        //execute consumer  
        se.execute(new Runnable(){  
            public void run() {  
                while(true){  
                    try {  
                        System.out.println("take== "+q.take()+" left:== ["+q.toString()+"]");  
                        try {  
                            TimeUnit.MILLISECONDS.sleep(r.nextInt(1000));  
                        } catch (InterruptedException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        }  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
        });  
        try {  
            TimeUnit.SECONDS.sleep(5);  
        } catch (InterruptedException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        System.out.println("shutdown");  
    }  

}  

class PriorityEntity implements Comparable<PriorityEntity> {  
    private static int count=0;  
    private int id=count++;  
    private int priority;  
    private int index=0;  

    public PriorityEntity(int priority,int index) {  
        this.priority = priority;  
        this.index=index;  
    }  

    public String toString(){  
        return id+"* [index="+index+" priority="+priority+"]";  
    }  


    //数字大,优先级高  
  public int compareTo(PriorityEntity o) {  
      return this.priority < o.priority ? 1  
              : this.priority > o.priority ? -1 : 0;  
  }  
} 
<小时/>

以下是结果,非常感谢您的帮助

enter image description here

最佳答案

一些观察:

  1. 在大多数情况下,队列的大小为 1。显然,这些排序顺序都无关紧要。
  2. 在某些情况下,队列大小可能为2,并且在这种情况下,输出不会暗示优先级较低的元素是首选。我强调动词“暗示”是因为......
  3. 您的代码没有同步块(synchronized block),因此没有任何东西可以阻止以下操作序列:

    q.take();     // consumer thread
    q.put();      // producer thread
    q.toString(); // consumer thread
    

    q.toString() 的合法结果显示比所采用的优先级更高的元素。

关于java - 为什么PriorityBlockingQueue队列没有​​按照优先级对元素进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32249618/

相关文章:

java - 如何在 Java 中追加数据包?

Java Lucene 类路径问题 ubuntu

带有 Boss-Worker 的 Java 线程

java - 将资源打包成Jar并访问

java - 如何从有序列表中选择随机起始位置?

c++ - 无锁容器如何对并发分区和排序使用react?

Kotlin 协同例程按顺序执行,但仅在生产机器上执行

java - ArrayBlockingQueue 和添加 vs 放置 vs 容量

hystrix - 当我已经有 max.Queue.poolSize 时,Hystrix 中的queueSizeRejectionThreshold 有什么用?

java - java BlockingQueue 实现中的 while 循环