1. 程式人生 > 其它 >Android自定義執行緒池——轉載

Android自定義執行緒池——轉載

定義執行緒優先順序列舉
/**
 * 執行緒優先順序
 */
public enum Priority {
    HIGH, NORMAL, LOW
}
定義執行緒任務
/**
 * 帶有優先順序的Runnable型別
 */
/*package*/ class PriorityRunnable implements Runnable {

    public final Priority priority;//任務優先順序
    private final Runnable runnable;//任務真正執行者
    /*package*/ long SEQ;//任務唯一標示

    public PriorityRunnable(Priority priority, Runnable runnable) {
        
this.priority = priority == null ? Priority.NORMAL : priority; this.runnable = runnable; } @Override public final void run() { this.runnable.run(); } }
定義一個PriorityExecutor繼承ThreadPoolExecutor
public class PriorityExecutor extends ThreadPoolExecutor {

    private static
final int CORE_POOL_SIZE = 5;//核心執行緒池大小 private static final int MAXIMUM_POOL_SIZE = 256;//最大執行緒池佇列大小 private static final int KEEP_ALIVE = 1;//保持存活時間,當執行緒數大於corePoolSize的空閒執行緒能保持的最大時間。 private static final AtomicLong SEQ_SEED = new AtomicLong(0);//主要獲取新增任務 /** * 建立執行緒工廠 */ private
static final ThreadFactory sThreadFactory = new ThreadFactory() { private final AtomicInteger mCount = new AtomicInteger(1); @Override public Thread newThread(Runnable runnable) { return new Thread(runnable, "download#" + mCount.getAndIncrement()); } }; /** * 執行緒佇列方式 先進先出 */ private static final Comparator<Runnable> FIFO = new Comparator<Runnable>() { @Override public int compare(Runnable lhs, Runnable rhs) { if (lhs instanceof PriorityRunnable && rhs instanceof PriorityRunnable) { PriorityRunnable lpr = ((PriorityRunnable) lhs); PriorityRunnable rpr = ((PriorityRunnable) rhs); int result = lpr.priority.ordinal() - rpr.priority.ordinal(); return result == 0 ? (int) (lpr.SEQ - rpr.SEQ) : result; } else { return 0; } } }; /** * 執行緒佇列方式 後進先出 */ private static final Comparator<Runnable> LIFO = new Comparator<Runnable>() { @Override public int compare(Runnable lhs, Runnable rhs) { if (lhs instanceof PriorityRunnable && rhs instanceof PriorityRunnable) { PriorityRunnable lpr = ((PriorityRunnable) lhs); PriorityRunnable rpr = ((PriorityRunnable) rhs); int result = lpr.priority.ordinal() - rpr.priority.ordinal(); return result == 0 ? (int) (rpr.SEQ - lpr.SEQ) : result; } else { return 0; } } }; /** * 預設工作執行緒數5 * * @param fifo 優先順序相同時, 等待佇列的是否優先執行先加入的任務. */ public PriorityExecutor(boolean fifo) { this(CORE_POOL_SIZE, fifo); } /** * @param poolSize 工作執行緒數 * @param fifo 優先順序相同時, 等待佇列的是否優先執行先加入的任務. */ public PriorityExecutor(int poolSize, boolean fifo) { this(poolSize, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>(MAXIMUM_POOL_SIZE, fifo ? FIFO : LIFO), sThreadFactory); } public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); } /** * 判斷當前執行緒池是否繁忙 * @return */ public boolean isBusy() { return getActiveCount() >= getCorePoolSize(); } /** * 提交任務 * @param runnable */ @Override public void execute(Runnable runnable) { if (runnable instanceof PriorityRunnable) { ((PriorityRunnable) runnable).SEQ = SEQ_SEED.getAndIncrement(); } super.execute(runnable); } }
測試程式
ExecutorService executorService = new PriorityExecutor(5, false);
        for (int i = 0; i < 20; i++) {
            PriorityRunnable priorityRunnable = new PriorityRunnable(Priority.NORMAL, new Runnable() {
                @Override
                public void run() {
                    Log.e(TAG, Thread.currentThread().getName()+"優先順序正常");
                }
            });
            if (i % 3 == 1) {
                priorityRunnable = new PriorityRunnable(Priority.HIGH, new Runnable() {
                    @Override
                    public void run() {
                        Log.e(TAG, Thread.currentThread().getName()+"優先順序高");
                    }
                });
            } else if (i % 5 == 0) {
                priorityRunnable = new PriorityRunnable(Priority.LOW, new Runnable() {
                    @Override
                    public void run() {
                        Log.e(TAG, Thread.currentThread().getName()+"優先順序低");
                    }
                });
            }
            executorService.execute(priorityRunnable);
        }

最後

1、把run的那一塊換成你自己的多執行緒顯示程式碼。

2、轉載地址:https://www.cnblogs.com/whoislcj/p/5610903.html