线程池通用工具类

package com.yhlz.utils;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.*;

/**
 * @author 
 */
public class ThreadPoolUtil {

    /**
     * @javadoc 线程池维护线程的最少数量
     */
    private static final int SIZE_CORE_POOL = Runtime.getRuntime().availableProcessors();
    /**
     * @javadoc线程池维护线程的最大数量,CPU核心数*2
     */
    private static final int SIZE_MAX_POOL = Runtime.getRuntime().availableProcessors() * 2;
    /**
     * 线程池维护线程所允许的空闲时间
     */
    private static final int TIME_KEEP_ALIVE = 60 * 30;
    /**
     * 线程池所使用的缓冲队列大小
     */
    private static final int SIZE_WORK_QUEUE = 100;
    /**
     * 任务调度周期
     */
    private static final int PERIOD_TASK_QOS = 1000;

    private static ThreadPoolUtil single = new ThreadPoolUtil();

    /**
     * 线程池单例创建方法
     */
    public static ThreadPoolUtil newInstance() {
        return single;
    }

    /**
     * 任务缓冲队列
     */
    private final Queue<Runnable> mTaskQueue = new LinkedList<Runnable>();

    /**
     * 线程池超出界线时将任务加入缓冲队列
     */
    private final RejectedExecutionHandler mHandler = new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
            mTaskQueue.offer(task);
        }
    };

    /**
     * 将缓冲队列中的任务重新加载到线程池
     */
    private final Runnable mAccessBufferThread = new Runnable() {
        @Override
        public void run() {
            if (hasMoreAcquire()) {
                mThreadPool.execute(mTaskQueue.poll());
            }
        }
    };
    /**
     * 创建一个调度线程池
     */
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    /**
     * 通过调度线程周期性的执行缓冲队列中任务
     */
    protected final ScheduledFuture<?> mTaskHandler = scheduler.scheduleAtFixedRate(mAccessBufferThread, 0,
            PERIOD_TASK_QOS, TimeUnit.MILLISECONDS);
    /**
     * 线程池
     */
    private final ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(SIZE_CORE_POOL, SIZE_MAX_POOL,
            TIME_KEEP_ALIVE, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(SIZE_WORK_QUEUE), mHandler);

    /**
     * 将构造方法访问修饰符设为私有,禁止任意实例化。
     */
    private ThreadPoolUtil() {
    }

    public void perpare() {
        if (mThreadPool.isShutdown() && !mThreadPool.prestartCoreThread()) {
            @SuppressWarnings("unused")
            int startThread = mThreadPool.prestartAllCoreThreads();
        }
    }

    /**
     * 消息队列检查方法
     */
    private boolean hasMoreAcquire() {
        return !mTaskQueue.isEmpty();
    }

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

    protected boolean isTaskEnd() {
        if (mThreadPool.getActiveCount() == 0) {
            return true;
        } else {
            return false;
        }
    }

    public void shutdown() {
        mTaskQueue.clear();
        mThreadPool.shutdown();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值