分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

NoHttp封装--06 NoHttp之队列、队列优先级

发布时间:2023-09-06 01:54责任编辑:赖小花关键词:暂无标签
public class Main { ???/** ????* 程序入口 ????*/ ???public void start() { ???????// 第一种,先进先出的队列 ???????// YolandaLinkedQueue queue = new YolandaLinkedQueue(3); ???????// queue.start(); ???????// 第二种,没有顺序的队列 ???????YolandaQueue queue = new YolandaQueue(1); ???????queue.start(); ???????// 往队列中添加请求 ???????for (int i = 0; i < 20; i++) { ???????????Request request = new Request("请求" + i); ???????????if (i == 10) ???????????????request.setPriority(Priority.C); ???????????if (i == 15) ???????????????request.setPriority(Priority.D); ???????????queue.add(request); ???????} ???} ???public static void main(String[] args) { ???????Main main = new Main(); ???????main.start(); ???}}public class YolandaLinkedQueue { ???private BlockingQueue<Request> blockingQueue; ???private TaskExecutor[] taskExecutors; ???public YolandaLinkedQueue(int poolSize) { ???????// LinkedBlockingQueue是一个先进先出的队列 ???????blockingQueue = new LinkedBlockingQueue<>(); ???????taskExecutors = new TaskExecutor[poolSize]; ???} ???public void add(Request request) { ???????blockingQueue.add(request); ???} ???public void start() { ???????for (int i = 0; i < taskExecutors.length; i++) { ???????????taskExecutors[i] = new TaskExecutor(blockingQueue); ???????????taskExecutors[i].start(); ???????} ???} ???public void stop() { ???????for (TaskExecutor taskExecutor : taskExecutors) { ???????????taskExecutor.setRunning(false); ???????????taskExecutor.interrupt(); ???????} ???}}public class YolandaQueue { ???private BlockingQueue<Request> blockingQueue; ???private TaskExecutor[] taskExecutors; ???private AtomicInteger atomicInteger = new AtomicInteger(); ???public YolandaQueue(int poolSize) { ???????// 如果Comparable#compareTo(Object)方法不做比较返回0,那么是无序的 ???????blockingQueue = new PriorityBlockingQueue<Request>(); ???????taskExecutors = new TaskExecutor[poolSize]; ???} ???public void add(Request request) { ???????request.setOrder(atomicInteger.incrementAndGet()); ???????blockingQueue.add(request); ???} ???public void start() { ???????for (int i = 0; i < taskExecutors.length; i++) { ???????????taskExecutors[i] = new TaskExecutor(blockingQueue); ???????????taskExecutors[i].start(); ???????} ???} ???public void stop() { ???????for (TaskExecutor taskExecutor : taskExecutors) { ???????????taskExecutor.setRunning(false); ???????????taskExecutor.interrupt(); ???????} ???}}public class TaskExecutor extends Thread { ???private BlockingQueue<Request> blockingQueue; ???private boolean isRunning = true; ???public TaskExecutor(BlockingQueue<Request> blockingQueue) { ???????this.blockingQueue = blockingQueue; ???} ???/** ????* @param isRunning the isRunning to set ????*/ ???public void setRunning(boolean isRunning) { ???????this.isRunning = isRunning; ???} ???@Override ???public void run() { ???????while (isRunning) { ???????????Request request = null; ???????????try { ???????????????// take方法是一个阻塞的方法,每次调用会拿到队列中的第一个任务,如果队列为空,这个方法将一直阻塞,知道队列中有任务再次返回 ???????????????request = blockingQueue.take(); ???????????} catch (InterruptedException e) { ???????????????return; ???????????} ???????????try { ???????????????Thread.sleep(1000); ???????????} catch (InterruptedException e) { ???????????????e.printStackTrace(); ???????????} ???????????System.out.println(request.getName()); ???????} ???}}public class Request implements Comparable<Request> { ???private String name; ???private Priority mPriority = Priority.B; ???private int order; ???/** ????* @param name ????*/ ???public Request(String name) { ???????super(); ???????this.name = name; ???} ???/** ????* @return the name ????*/ ???public String getName() { ???????return name; ???} ???/** ????* @param name the name to set ????*/ ???public void setName(String name) { ???????this.name = name; ???} ???/** ????* @param mPriority the mPriority to set ????*/ ???public void setPriority(Priority mPriority) { ???????this.mPriority = mPriority; ???} ???/** ????* @return the mPriority ????*/ ???public Priority getPriority() { ???????return mPriority; ???} ???/** ????* @return the order ????*/ ???public int getOrder() { ???????return order; ???} ???/** ????* @param order the order to set ????*/ ???public void setOrder(int order) { ???????this.order = order; ???} ???@Override ???public int compareTo(Request other) { ???????// 返回正数代表1排在2后面,返回负数表示1排在2前面 ???????Priority priority = getPriority();// 拿到自身的优先级 ???????Priority otherPriority = other.getPriority(); ???????return priority == otherPriority ? getOrder() - other.getOrder() : otherPriority.ordinal() - priority.ordinal(); ???}}public enum Priority { ???/** ????* 优先级最低 ????*/ ???A, ???/** ????* 默认优先级 ????*/ ???B, ???/** ????* 优先级最高 ????*/ ???C, ???/** ????* 一般情况下不用;特殊情况下,请求假如到到队列后立即执行 ????*/ ???D}

NoHttp封装--06 NoHttp之队列、队列优先级

原文地址:https://www.cnblogs.com/ganchuanpu/p/9038596.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved