摘要:程序入口第一种,先进先出的队列第二种,没有顺序的队列往队列中添加请求请求是一个先进先出的队列如果方法不做比较返回,那么是无序的方法是一个阻塞的方法,每次调用会拿到队列中的第一个任务,如果队列为空,这个方法将一直阻塞,知道队列中有任务再次返回
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 BlockingQueueblockingQueue; 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 blockingQueue; private TaskExecutor[] taskExecutors; private AtomicInteger atomicInteger = new AtomicInteger(); public YolandaQueue(int poolSize) { // 如果Comparable#compareTo(Object)方法不做比较返回0,那么是无序的 blockingQueue = new PriorityBlockingQueue (); 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 blockingQueue; private boolean isRunning = true; public TaskExecutor(BlockingQueue 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 { 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 }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/3069.html
摘要:登陆退出处理登录结果登录接口数据处理登出结果退出接口数据请求失败添加一个请求到请求队列上下文请求对象接受回调结果,当多个请求用同一个接受结果时,用来区分请求是否显示请求是否能被用户取消是否提示用户错误信息客户端错误客户端发生错误服务器错误服NoHttpActivity 1 public class NoHttpActivity extends Activity implements Vi...
摘要:下载下载按钮暂停开始等下载状态下载进度条下载地址下载请求下载地址保存的文件夹文件名是否断点续传下载如果发现文件已经存在是否删除后重新下载检查已经下载了一半的文件是什么状态文件已经下载完成下载完成已经下载完成提示用户安装代表文件不存在,需要从xml java: 1 import com.yolanda.nohttp.Headers; 2 ...
摘要:请求,缓存非标准协议,改变缓存模式为请求图片,缓存图片仅仅请求网络无论如何也只会请求网络,也不支持这种默认行为。仅仅读取缓存无论如何仅仅读取缓存,不会请求网络和其它操作。 1、Default模式,也是没有设置缓存模式时的默认模式 这个模式实现http协议中的内容,比如响应码是304时,当然还会结合E-Tag和LastModify等头。 StringRequest request = ne...
摘要:实体类请求针对的具体在中使用处理登录结果登录接口结果处理登出结果退出接口结果请求失败可以解析所有的自定义请求但是前提是传进来的必须提供了默认实现bean实体类请求: 1.bean 1 import java.io.Serializable; 2 import com.alibaba.fastjson.annotation.JSONField; 3 4 public class U...
摘要:请求自动维持支持临时的位置。支持重启关机开机后继续持久化维持。提供了接口,允许开发者监听的变化,也可以改变某个的值。服务器端登录成功登录失败客户端登录按钮成功了成功时拿到头这里就拿到了你想那的失败了文章NoHttp请求自动维持Cookie: 1.支持Session、Cookie、临时Cookie的位置。 2.支持App重启、关机开机后继续持久化维持。 3.提供了接口,允许开发者监...
阅读 2756·2021-11-12 10:36
阅读 2274·2021-11-02 14:43
阅读 2016·2019-08-30 14:23
阅读 3335·2019-08-30 13:08
阅读 717·2019-08-28 18:09
阅读 2988·2019-08-26 12:22
阅读 2920·2019-08-23 18:24
阅读 1873·2019-08-23 18:17