|
| 1 | +package com.isoftstone.huidingc.testqmui.thread; |
| 2 | + |
| 3 | +import android.support.annotation.NonNull; |
| 4 | + |
| 5 | +import java.util.Comparator; |
| 6 | +import java.util.concurrent.BlockingQueue; |
| 7 | +import java.util.concurrent.PriorityBlockingQueue; |
| 8 | +import java.util.concurrent.RejectedExecutionHandler; |
| 9 | +import java.util.concurrent.ThreadFactory; |
| 10 | +import java.util.concurrent.ThreadPoolExecutor; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | +import java.util.concurrent.atomic.AtomicInteger; |
| 13 | +import java.util.concurrent.atomic.AtomicLong; |
| 14 | + |
| 15 | +/** |
| 16 | + * @auther huidingc |
| 17 | + * @date 2018/3/16 15:00 |
| 18 | + * @description PriorityExecutor |
| 19 | + * 创建线程池 |
| 20 | + */ |
| 21 | +public class PriorityExecutor extends ThreadPoolExecutor { |
| 22 | + /** |
| 23 | + * 线程池维护线程的最少数量 |
| 24 | + * 核心线程池大小 |
| 25 | + */ |
| 26 | + private static final int CORE_POOL_SIZE = 5; |
| 27 | + /** |
| 28 | + * 最大线程池队列大小 |
| 29 | + */ |
| 30 | + private static final int MAXIMUM_POOL_SIZE = 256; |
| 31 | + /** |
| 32 | + * 保持存活时间,当线程数大于corePoolSize的空闲线程能保持的最大时间。 |
| 33 | + */ |
| 34 | + private static final int KEEP_ALIVE_TIME = 1; |
| 35 | + /** |
| 36 | + * 主要获取添加任务 |
| 37 | + */ |
| 38 | + private static final AtomicLong SEQ_SEED = new AtomicLong(0); |
| 39 | + |
| 40 | + |
| 41 | + public PriorityExecutor(boolean fifo){ |
| 42 | + this(CORE_POOL_SIZE,fifo); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * 常见几种BlockingQueue实现 |
| 47 | + * 1. ArrayBlockingQueue : 有界的数组队列 |
| 48 | + * 2. LinkedBlockingQueue : 可支持有界/无界的队列,使用链表实现 |
| 49 | + * 3. PriorityBlockingQueue : 优先队列,可以针对任务排序 |
| 50 | + * 4. SynchronousQueue : 队列长度为1的队列,和Array有点区别就是:client thread提交到block queue会是 |
| 51 | + * 一个阻塞过程,直到有一个worker thread连接上来poll task。 |
| 52 | + * @param poolSize |
| 53 | + * @param fifo |
| 54 | + */ |
| 55 | + public PriorityExecutor(int poolSize, boolean fifo) { |
| 56 | + this(poolSize, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>(MAXIMUM_POOL_SIZE, fifo ? FIFO : LIFO), threadFactory); |
| 57 | + } |
| 58 | + |
| 59 | + public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { |
| 60 | + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); |
| 61 | + } |
| 62 | + |
| 63 | + public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { |
| 64 | + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); |
| 65 | + } |
| 66 | + |
| 67 | + public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { |
| 68 | + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); |
| 69 | + } |
| 70 | + |
| 71 | + public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { |
| 72 | + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * 创建线程工厂 |
| 77 | + */ |
| 78 | + private static ThreadFactory threadFactory = new ThreadFactory() { |
| 79 | + private AtomicInteger mCount = new AtomicInteger(1); |
| 80 | + |
| 81 | + @Override |
| 82 | + public Thread newThread(@NonNull Runnable r) { |
| 83 | + return new Thread(r,"download#"+mCount.getAndIncrement()); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + /** |
| 88 | + * 线程队列方式,先进先出 |
| 89 | + */ |
| 90 | + private static Comparator<Runnable> FIFO = new Comparator<Runnable>() { |
| 91 | + @Override |
| 92 | + public int compare(Runnable runnable1, Runnable runnable2) { |
| 93 | + if(runnable1 instanceof PriorityRunnable && runnable2 instanceof PriorityRunnable){ |
| 94 | + PriorityRunnable r1 = (PriorityRunnable) runnable1; |
| 95 | + PriorityRunnable r2 = (PriorityRunnable) runnable2; |
| 96 | + int result = r1.priority.ordinal() - r2.priority.ordinal(); |
| 97 | + return result == 0 ? (int) (r1.SEQ - r2.SEQ) : result; |
| 98 | + }else{ |
| 99 | + return 0; |
| 100 | + } |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + /** |
| 105 | + * 线程队列方式,后进先出 |
| 106 | + */ |
| 107 | + private static Comparator<Runnable> LIFO = new Comparator<Runnable>() { |
| 108 | + @Override |
| 109 | + public int compare(Runnable runnable1, Runnable runnable2) { |
| 110 | + if(runnable1 instanceof PriorityRunnable && runnable2 instanceof PriorityRunnable){ |
| 111 | + PriorityRunnable r1 = (PriorityRunnable) runnable1; |
| 112 | + PriorityRunnable r2 = (PriorityRunnable) runnable2; |
| 113 | + int result = r1.priority.ordinal() - r2.priority.ordinal(); |
| 114 | + return result == 0 ? (int) (r2.SEQ - r1.SEQ) : result; |
| 115 | + }else{ |
| 116 | + return 0; |
| 117 | + } |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + /** |
| 122 | + * 判断当前线程是否繁忙 |
| 123 | + * @return |
| 124 | + */ |
| 125 | + public boolean isBusy(){ |
| 126 | + return getActiveCount() >= getCorePoolSize(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * 提交任务 |
| 131 | + * @param runnable |
| 132 | + */ |
| 133 | + @Override |
| 134 | + public void execute(Runnable runnable) { |
| 135 | + if(runnable instanceof PriorityRunnable){ |
| 136 | + ((PriorityRunnable) runnable).SEQ = SEQ_SEED.getAndIncrement(); |
| 137 | + } |
| 138 | + super.execute(runnable); |
| 139 | + } |
| 140 | +} |
0 commit comments