ProiorityBlockingQueue

优先级队列,线程安全队列。
java

@Test
    public void priority() {
        PriorityBlockingQueue testQueue = new PriorityBlockingQueue<>();
        testQueue.offer(new Data(121234411));
        testQueue.offer(new Data(12));
        testQueue.offer(new Data(13));
        testQueue.offer(new Data(14));
        testQueue.offer(new Data(12123));
        testQueue.offer(new Data(12234));


        while (testQueue.size() > 0) {
            System.out.println(testQueue.poll().getId());
        }

    }

    @Getter
    public static class Data implements Comparable{
        private final Integer id;

        public Data(Integer id) {
            this.id = id;
        }

        @Override
        public int compareTo(Data o) {
            return o.getId().compareTo(this.getId());
        }
    }
  • output
Connected to the target VM, address: '127.0.0.1:49644', transport: 'socket'
121234411
12234
12123
14
13
12
Disconnected from the target VM, address: '127.0.0.1:49644', transport: 'socket'

Process finished with exit code 0

你可能感兴趣的:(ProiorityBlockingQueue)