package com.gf.conn013;

import java.util.Iterator;
import java.util.concurrent.PriorityBlockingQueue; /**
*
* PriorityBlockingQueue: 基于优先级的阻塞队列(优先级的判断通过构造函数传入的的Compator对象决定,也就是传入的队列的对象必须实现Comparable接口)
* 在实现PriorityBlockiongQueue时,内部控制线程同步的锁采用的是公平锁
* 它也是一个无界队列
* 虽然遍历队列依然是,放入的顺序 ,但是在通过take()方法获取数据时,获取的是通过比较规则排列的数据
*
* @author huanchu
*/
public class UsePriorityBlockingQueue { public static void main(String[] args) throws InterruptedException { PriorityBlockingQueue<Task> q = new PriorityBlockingQueue<Task>(); Task t1 = new Task();
t1.setId(3);
t1.setName("任务1 ,id=3"); Task t2 = new Task();
t2.setId(6);
t2.setName("任务2 ,id=6"); Task t3 = new Task();
t3.setId(1);
t3.setName("任务3 ,id=1"); q.add(t1);
q.add(t2);
q.add(t3); //直接for循环变量,队列中的元素是不会按我们想要顺序遍历出来
// for (Iterator iterator = q.iterator(); iterator.hasNext();) {
// Task task = (Task) iterator.next();
// System.out.println(task.getName());
//
// }
q.take();//每次取出的元素,则是站在我们的的比较规则排列的顺序,取出元素 } }

  

package com.gf.conn013;

public class Task implements Comparable<Task>{

	private int id;
private String name; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Task [id=" + id + ", name=" + name + "]";
} @Override
public int compareTo(Task task) { return this.id > task.id ? 1 : (this.id < task.id ? -1 : 0);
} }

关注我的公众号,精彩内容不能错过

3. 基于优先级的Queue(PriorityBlockingQueue)的更多相关文章

  1. java PriorityBlockingQueue 基于优先级队列,的读出操作可以阻止.

    java PriorityBlockingQueue 基于优先级队列.的读出操作可以阻止. package org.rui.thread.newc; import java.util.ArrayLis ...

  2. VxWorks实验六 基于优先级的抢占式调度及实验的源程序和实验步骤

    基于优先级的抢占式调度及实验的源程序和实验步骤 1 实验目的    1.学习并验证基于优先级的抢占式调度2 实验内容    在实验一建立的 project 中,创建3 个任务,对这三个任务使用基于优先 ...

  3. Azure Traffic Manager(二) 基于权重与基于优先级的路由策略为我们的Web项目提供负载均衡

    一,引言 上一片文章我们使用 Azure Traffic Manager 分发用户请求,同时演示了两种路由策略,“Performance”,“Geographic”的两种方式,今天我们继续讲解 Tra ...

  4. 算法:基于 RingBuffer 的 Queue 实现

    背景 如果基于数组实现队列,常见的选择是采用 RingBuffer,否则就需要移动数组元素. RingBuffer 很容易看出 RingBuffer 的思想,这里就不赘述了. 您可以思考一个问题:图中 ...

  5. 算法:基于 RingBuffer 的 Queue 实现《续》

    背景 上篇实现了一个简单的队列,内部使用了 _count 计数,本文采用另外一种模式,不用 _count 计数. RingBuffer 不用 _count 计数的话,为了区分队列的满和空,需要在数组中 ...

  6. 用Java如何设计一个阻塞队列,然后说说ArrayBlockingQueue和LinkedBlockingQueue

    前言 用Java如何设计一个阻塞队列,这个问题是在面滴滴的时候被问到的.当时确实没回答好,只是说了用个List,然后消费者再用个死循环一直去监控list的是否有值,有值的话就处理List里面的内容.回 ...

  7. JS数据结构及算法(二) 队列

    队列是遵循先进先出的一种数据结构,在尾部添加新元素,并从顶部移除元素. 1.普通队列 function Queue() { this.items = []; } Queue.prototype = { ...

  8. atitit. java queue 队列体系and自定义基于数据库的队列总结o7t

    atitit. java queue 队列体系and自定义基于数据库的队列总结o7t 1. 阻塞队列和非阻塞队列 1 2. java.util.Queue接口, 1 3. ConcurrentLink ...

  9. atitit. java queue 队列体系and自己定义基于数据库的队列总结o7t

    atitit. java queue 队列体系and自己定义基于数据库的队列总结o7t 1. 堵塞队列和非堵塞队列 1 2. java.util.Queue接口. 1 3. ConcurrentLin ...

随机推荐

  1. go语言指针理解

  2. VBS列出windows更新列表

    Set objSession = CreateObject("Microsoft.Update.Session") Set objSearcher = objSession.Cre ...

  3. filter 过滤器 禁止浏览器缓存

    public class BrowserNoCacheFilter implements Filter { public void init(FilterConfig filterconfig) th ...

  4. 【从零开始搭建自己的.NET Core Api框架】(六)泛型仓储的作用

    系列目录 一.  创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...

  5. [Swift]LeetCode163. 缺失区间 $ Missing Ranges

    Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...

  6. [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  7. [Swift]LeetCode1013. 将数组分成和相等的三个部分 | Partition Array Into Three Parts With Equal Sum

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...

  8. Elasticsearch基础知识分享

    1. Elasticsearch背景介绍 Elasticsearch 是一个基于 Lucene 的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful web 接口.Elast ...

  9. 5.jQuery

    简介 jQuery是一个快速.简洁的JavaScript框架,jQuery设计的宗旨是“write Less,Do More”,即倡导写更少的代码,做更多的事情.它封装JavaScript常用的功能代 ...

  10. java代码之美(1)---Lambda

    Lambda 一.概述 1.什么是Lambda表达式 Lambda 表达式是一种匿名函数,简单地说,它是没有声明的方法,也即没有访问修饰符.返回值声明和名字. 它可以写出更简洁.更灵活的代码.作为一种 ...