博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java多线程之新类库中的构件PriorityBlockingQueue
阅读量:5244 次
发布时间:2019-06-14

本文共 4455 字,大约阅读时间需要 14 分钟。

 

package concurrent2;import java.util.ArrayList;  import java.util.List;  import java.util.Queue;  import java.util.Random;  import java.util.concurrent.ExecutorService;  import java.util.concurrent.Executors;  import java.util.concurrent.PriorityBlockingQueue;  import java.util.concurrent.TimeUnit;      class PrioritizedTask implements Runnable, Comparable
{ private Random rand = new Random(47); private static int counter = 0; private final int id = counter++; private final int priority; protected static List
sequence = new ArrayList
(); public PrioritizedTask(int priority) { this.priority = priority; sequence.add(this); } @Override public int compareTo(PrioritizedTask o) { //复写此方法进行任务执行优先级排序 // return priority < o.priority ? 1 : // (priority > o.priority ? -1 : 0); if(priority < o.priority) { return -1; }else { if(priority > o.priority) { return 1; }else { return 0; } } } @Override public void run() { //执行任务代码.. try { TimeUnit.MILLISECONDS.sleep(rand.nextInt(250)); } catch (InterruptedException e) { } System.out.println(this); } @Override public String toString() { return String.format("[%1$-3d]", priority) + " Task id : " + id; } public String summary() { return "( Task id : " + id + " _priority : " + priority + ")"; } /** * 结束所有任务 */ public static class EndSentinel extends PrioritizedTask { private ExecutorService exec; public EndSentinel(ExecutorService e) { super(Integer.MAX_VALUE); exec = e; } public void run() { int count = 0; for(PrioritizedTask pt : sequence) { System.out.print(pt.summary()); if(++count % 5 == 0) { System.out.println(); } } System.out.println(); System.out.println(this + "Calling shutdownNow()"); exec.shutdownNow(); } } } /** * 制造一系列任务,分配任务优先级 */ class PrioritizedTaskProducer implements Runnable { private Random rand = new Random(47); private Queue
queue; private ExecutorService exec; public PrioritizedTaskProducer(Queue
q, ExecutorService e) { queue = q; exec = e; } @Override public void run() { for(int i = 0; i < 20; i++) { queue.add(new PrioritizedTask(rand.nextInt(10))); Thread.yield(); } try { for (int i = 0; i < 10; i++) { TimeUnit.MILLISECONDS.sleep(250); queue.add(new PrioritizedTask(10)); } for(int i = 0; i < 10; i++) { queue.add(new PrioritizedTask(i)); } queue.add(new PrioritizedTask.EndSentinel(exec)); } catch (InterruptedException e) { } System.out.println("Finished PrioritizedTaskProducer"); } } /** * 使用PriorityBlockingQueue进行任务按优先级同步执行 */ class PrioritizedTaskConsumer implements Runnable { private PriorityBlockingQueue
q; public PrioritizedTaskConsumer(PriorityBlockingQueue
q) { this.q = q; } @Override public void run() { try { while (!Thread.interrupted()) { q.take().run(); } } catch (InterruptedException e) { } System.out.println("Finished PrioritizedTaskConsumer"); } } public class PriorityBlockingQueueDemo { public static void main(String args[]) { ExecutorService exec = Executors.newCachedThreadPool(); PriorityBlockingQueue
queue = new PriorityBlockingQueue
(); exec.execute(new PrioritizedTaskProducer(queue, exec)); try { TimeUnit.MILLISECONDS.sleep(250); } catch (InterruptedException e) { } exec.execute(new PrioritizedTaskConsumer(queue)); } }

 

转载于:https://www.cnblogs.com/zhuawang/p/3784938.html

你可能感兴趣的文章
「Unity」委托 将方法作为参数传递
查看>>
重置GNOME-TERMINAL
查看>>
redis哨兵集群、docker入门
查看>>
hihoCoder 1233 : Boxes(盒子)
查看>>
oracle中anyData数据类型的使用实例
查看>>
软件测试——性能测试总结
查看>>
12.4站立会议
查看>>
客户端访问浏览器的流程
查看>>
codeforces水题100道 第二十二题 Codeforces Beta Round #89 (Div. 2) A. String Task (strings)
查看>>
c++||template
查看>>
[BZOJ 5323][Jxoi2018]游戏
查看>>
编程面试的10大算法概念汇总
查看>>
Vue
查看>>
python-三级菜单和购物车程序
查看>>
条件断点 符号断点
查看>>
水平垂直居中
查看>>
MySQL简介
查看>>
设计模式之桥接模式(Bridge)
查看>>
jquery的$(document).ready()和onload的加载顺序
查看>>
Python Web框架Django (五)
查看>>