首页 > 其他分享 >PriorityBlockingQueue 优先级队列

PriorityBlockingQueue 优先级队列

时间:2023-12-04 17:37:25浏览次数:33  
标签:优先级 队列 age queue Student new public PriorityBlockingQueue

package study;

import lombok.Data;

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

public class PriorityBlockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        PriorityBlockingQueue<Student> queue = new PriorityBlockingQueue<>(10, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        queue.put(new Student(1, "tom1"));
        queue.put(new Student(3, "tom3"));
        queue.put(new Student(2, "tom2"));

        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take());
    }

    @Data
    static
    class Student {
        private int age;
        private String name;

        public Student(int age, String name) {
            this.age = age;
            this.name = name;
        }
    }
}

标签:优先级,队列,age,queue,Student,new,public,PriorityBlockingQueue
From: https://www.cnblogs.com/lovedaodao/p/17875466.html

相关文章

  • 阻塞队列之 LinkedBlockingQueue
    LinkedBlockingQueue:Java多线程编程中的阻塞队列在Java多线程编程中,LinkedBlockingQueue是一个非常重要的类,它提供了一种用于在生产者和消费者之间进行数据传递的机制。LinkedBlockingQueue广泛应用于各种场景,如线程池、任务队列等。本文将详细介绍LinkedBlockingQueue的原理......
  • 消息队列入门 —— 以 Kafka 为例(一)
    消息队列入门——以Kafka为例(一)概述当我们的应用逐步变得庞大,各层应用之间调用关系越来越复杂,对系统的可用性以及可扩展性要求也越来越高。消息队列作为分布式系统架构中的一个关键中间件,提供了“消息传递”和“消息排队模型”,可以应用在系统解耦、异步处理、流量削峰等多个......
  • 栈和队列算法总结
    知识概览在数据结构中,栈和队列都属于线性表。栈是先进后出(FILO)的,队列是先进先出(FIFO)的。代码模板#include<iostream>usingnamespacestd;constintN=100010;//**********************栈intstk[N],tt;//插入stk[++tt]=x;//弹出tt--;//判断栈是否......
  • 四、Work Queues(工作队列)
    一、轮训分发消息1、抽取工具类2、启动两个工作线程2.1两个工作线程(消费者)2.2生产者3、启动一个发送线程4、结果展示二、消息应答1、概念2、自动应答3、消息应答的方法4、Multiple的解释5、消息自动重新入队6、消息手动应答代码......
  • 进程优先级详解
    Linux中采用了两种不同的优先级范围,一种是nice值,一种是实时优先级。在上一篇粗略的说了一下nice值和实时优先级,仍有不少疑问,本文来详细说明一下进程优先级。linux内核版本为linux2.6.34。进程优先级的相关信息,存放在进程描述符task_struct中:structtask_struct{......
  • RabbitMQ Java代码声明队列和交换机(方法一)
      交换机和队列的声明一般写在消费者模块里 代码示例:packagecom.itheima.config_RabbitMQ;importorg.springframework.amqp.core.*;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@Configuration......
  • FreeRTOS--队列集
    示例源码基于FreeRTOSV9.0.0队列集1.概述队列集的本质也是队列,只不过里面存放的是“队列句柄”。当任务需要及时读取多个队列时,可以使用队列集。它类似于posix的多路复用思想。可以将想要监听消息的队列放入队列集中,当其中有队列有数据达到时,队列集的接口会返回可读的队列句......
  • FreeRTOS--队列
    示例源码基于FreeRTOSV9.0.0队列1.概述FreeRTOS的队列,支持任务与任务间的通信,以及任务与中断间的通信。它是FreeRTOS系统中主要的任务间通信方式。队列内的消息,是通过拷贝方式传递,而非指针。队列除了基本的先进先出特性,也支持往队列首部写入数据。FreeRTOS基于队列进行扩......
  • RabbitMQ 接收队列的消息
     代码示例:注:要把这个类加上Component注解packagecom.itheima.amqp_listener;importorg.springframework.amqp.rabbit.annotation.RabbitListener;importorg.springframework.stereotype.Component;@ComponentpublicclassMQListener{@RabbitListener(queues="simpl......
  • RabbitMQ 发送消息到队列(交换机不参与的那种)
    1.导包<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>2.在application.yml文件里编写配置信息spring:rabbitmq:host:192.168.88.130port:5672......