线程池的简单封装

一个简单的封装,足够日常开发哦~

package com.cucc.voice.util;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: 线程池管理类
 * @author: zhaogx  qq:2673206184
 * @date: 2018/3/2 上午11:29
 */
public class ThreadPoolManager {

    private static final String TAG = "ThreadPoolManager";

    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
//    private static final int CORE_POOL_SIZE = 3;
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    private static final int KEEP_ALIVE = 1;

    private static final BlockingQueue<Runnable> sPoolWorkQueue =
            new LinkedBlockingQueue<>(128);

    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        //一个线程安全的Int类
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable r) {
            return new Thread(r, "ThreadPoolManager #" + mCount.getAndIncrement());
        }
    };
    /**
     * 单例模式
     */
    private static ThreadPoolManager manager;
    /**
     * 线程池对象
     */
    private ThreadPoolExecutor executor;

    /**
     * 私有构造,防止被初始化
     */
    private ThreadPoolManager() {
    }

    /**
     * 对外单例
     *
     * @return manager 操作类
     */
    public static ThreadPoolManager getInstance() {
        if (manager == null) {
            synchronized (ThreadPoolManager.class) {
                if (manager == null) {
                    manager = new ThreadPoolManager();
                }
            }
        }
        return manager;
    }

    /**
     * 执行
     *
     * @param r 执行对象
     */
    public void execute(Runnable r) {
        //为null时创建executor
        if (executor == null) {
            executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                    TimeUnit.MINUTES, sPoolWorkQueue, sThreadFactory) {
            };
        } else {
            //不为空,执行
            executor.execute(r);
        }
    }

    /**
     * 把线程从线程池中移除
     *
     * @param r 移除的对象
     */
    public void cancel(Runnable r) {
        if (executor != null) {
            executor.getQueue().remove(r);
        }
    }

    /**
     * 对外提供 ThreadPoolExecutor
     * @return executor
     */
    public ThreadPoolExecutor getThreadPool() {
        return executor;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值