線程池管理類ThreadPoolManager

package com.tools;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 線程池管理(線程統一調度管理)
 */
public final class ThreadPoolManager {

    private static ThreadPoolManager sThreadPoolManager = new ThreadPoolManager();

    // 線程池維護線程的最少數量
    private static final int SIZE_CORE_POOL = 5;

    // 線程池維護線程的最大數量
    private static final int SIZE_MAX_POOL = 10;

    /*
     * 線程池單例創建方法
     */
    public static ThreadPoolManager newInstance() {
        return sThreadPoolManager;
    }

    /**************************************************************************************************************
     * 常見的幾種線程技術
     **************************************************************************************************************
     * Java通過Executors提供四種線程池,分別爲:
     * newCachedThreadPool創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閒線程,若無可回收,則新建線程。
     * newFixedThreadPool 創建一個定長線程池,可控制線程最大併發數,超出的線程會在隊列中等待。
     * newScheduledThreadPool 創建一個定長線程池,支持定時及週期性任務執行。 newSingleThreadExecutor
     * 創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先級)執行。
     *
     * 1、public static ExecutorService newFixedThreadPool(int nThreads) {
     * return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
     *
     * 2、 public static ExecutorService newSingleThreadExecutor() {
     * return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
     *
     * 3、public static ExecutorService newCachedThreadPool() {
     * return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
     ****************************************************************************************************************/

    /**
     * 線程池
     * @param corePoolSize - 池中所保存的線程數,包括空閒線程。
     * @param maximumPoolSize - 池中允許的最大線程數。
     * @param keepAliveTime - 當線程數大於核心時,此爲終止前多餘的空閒線程等待新任務的最長時間。
     * @param unit - keepAliveTime 參數的時間單位。
     * @param workQueue - 執行前用於保持任務的隊列。此隊列僅由保持 execute 方法提交的 Runnable 任務。
     * @param handler - 由於超出線程範圍和隊列容量而使執行被阻塞時所使用的處理程序。
     */
    // 實質就是newFixedThreadPool 創建一個定長線程池,可控制線程最大併發數,超出的線程會在隊列中等待
    private final ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(SIZE_CORE_POOL, SIZE_MAX_POOL, 0L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

    /*
     * 將構造方法訪問修飾符設爲私有,禁止任意實例化。
     */
    private ThreadPoolManager() {
    }

    /*
     * 將線程池初始化,核心線程數量
     */
    public void perpare() {
        if (mThreadPool.isShutdown() && !mThreadPool.prestartCoreThread()) {
            @SuppressWarnings("unused")
            int startThread = mThreadPool.prestartAllCoreThreads();
        }
    }

    /*
     * 向線程池中添加任務方法
     */
    public void addExecuteTask(Runnable task) {
        if (task != null) {
            mThreadPool.execute(task);
        }
    }

    /*
     * 判斷是否是最後一個任務
     */
    protected boolean isTaskEnd() {
        if (mThreadPool.getActiveCount() == 0) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * 獲取緩存大小
     */
    public int getQueue(){
        return mThreadPool.getQueue().size();
    }

    /*
     * 獲取線程池中的線程數目
     */
    public int getPoolSize(){
        return mThreadPool.getPoolSize();
    }

    /*
     * 獲取已完成的任務數
     */
    public long getCompletedTaskCount(){
        return mThreadPool.getCompletedTaskCount();
    }

    /*
     * 關閉線程池,不在接受新的任務,會把已接受的任務執行玩
     */
    public void shutdown() {
        mThreadPool.shutdown();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章