线程池工厂

举报
longlinji 发表于 2021/01/19 15:59:34 2021/01/19
【摘要】 ThreadPoolFactory.getNormalPool.submit(()->{});public class ThreadPoolProxy implements Executor { /** * The Constant LOGGER. */ private static final Logger LOGGER = LoggerFactory.getL...

ThreadPoolFactory.getNormalPool.submit(
()->{

});

public class ThreadPoolProxy implements Executor {
    /**
     * The Constant LOGGER.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadPoolProxy.class);

    /**
     * The m executor.
     */
    ThreadPoolExecutor mExecutor; // 只需创建一次

    /**
     * The m core pool size.
     */
    int mCorePoolSize;

    /**
     * The m maximum pool size.
     */
    int mMaximumPoolSize;

    /**
     * The m keep alive time.
     */
    long mKeepAliveTime;

    /**
     * Instantiates a new thread pool proxy.
     *
     * @param corePoolSize the core pool size
     * @param maximumPoolSize the maximum pool size
     * @param keepAliveTime the keep alive time
     * @return null
     */
    public ThreadPoolProxy(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
        mCorePoolSize = corePoolSize;
        mMaximumPoolSize = maximumPoolSize;
        mKeepAliveTime = keepAliveTime;
        initThreadPoolExecutor();
    }

    /**
     * Inits the thread pool executor.
     *
     * @return the thread pool executor
     */
    private ThreadPoolExecutor initThreadPoolExecutor() {
        // 双重检查加锁
        if (mExecutor == null) {
            synchronized (ThreadPoolProxy.class) {
                if (mExecutor == null) {
                    TimeUnit unit = TimeUnit.SECONDS;
                    SynchronousQueue<Runnable> workQueue = new SynchronousQueue<Runnable>(); // 阻塞队列
                    mExecutor = new ThreadPoolExecutor( //
                        mCorePoolSize, // 核心的线程数
                        mMaximumPoolSize, // 最大的线程数
                        mKeepAliveTime, // 保持时间
                        unit, // 保持时间对应的单位
                        workQueue, // 缓存队列/阻塞队列
                        (runnable, executors) -> {
                            try {
                                executors.getQueue().put(runnable);
                            } catch (InterruptedException e) {
                                LOGGER.error("The request blocking queue is Exception={}", e);
                            }
                        });
                }
            }
        }
        return mExecutor;
    }

    /**
     * Execute.
     *
     * @param task the task
     */
    @Override
    public void execute(Runnable task) {
        mExecutor.execute(task);
    }

    /**
     * Submit task.
     *
     * @param task the task
     */
    public void submitTask(Runnable task) {
        mExecutor.submit(task);
    }

    /**
     * Submit.
     *
     * @param task the task
     * @return the future
     */
    public Future<?> submit(Runnable task) {
        return mExecutor.submit(task);
    }

    /**
     * Removes the task.
     *
     * @param task the task
     */
    public void removeTask(Runnable task) {
        mExecutor.remove(task);
    }

    /**
     * Gets the pool size.
     *
     * @return the pool size
     */
    public int getPoolSize() {
        return mExecutor.getPoolSize();
    }

    /**
     * Gets the queue size.
     *
     * @return the queue size
     */
    public int getQueueSize() {
        return mExecutor.getQueue().size();
    }

    /**
     * Gets the completed task count.
     *
     * @return the completed task count
     */
    public long getCompletedTaskCount() {
        return mExecutor.getCompletedTaskCount();
    }

    /**
     * Gets the active count.
     *
     * @return the active count
     */
    public long getActiveCount() {
        return mExecutor.getActiveCount();
    }

    /**
     * Gets the task count.
     *
     * @return the task count
     */
    public long getTaskCount() {
        return mExecutor.getTaskCount();
    }

    /**
     * Checks if is pool full used.
     *
     * @return true, if is pool full used
     */
    public boolean isPoolFullUsed() {
        return mExecutor.getActiveCount() == mMaximumPoolSize;
    }

    /**
     * Destory.
     */
    public void destory() {
        try {
            mExecutor.shutdown();
        } catch (Exception e) {
            LOGGER.error("destory executor pool shutdown fail", e);
        }
    }
}
public class ThreadPoolFactory {

    /**
     * The m normal pool.
     */
    static ThreadPoolProxy mNormalPool;

    /**
     * The mOasPool使用
     */
    static ThreadPoolProxy mOasPool;

    /**
     * Gets the uom pool.
     *
     * @return the uom pool
     */
    public static ThreadPoolProxy getOASPool() {
        if (mOasPool == null) {
            synchronized (ThreadPoolProxy.class) {
                if (mOasPool == null) {
                    mOasPool = new ThreadPoolProxy(50, 400, 300);
                }
            }
        }
        return mOasPool;
    }

    /**
     * Gets the normal pool.
     *
     * @return the normal pool
     */
    public static ThreadPoolProxy getNormalPool() {
        if (mNormalPool == null) {
            synchronized (ThreadPoolProxy.class) {
                if (mNormalPool == null) {
                    mNormalPool = new ThreadPoolProxy(20, 50, 300);
                }
            }
        }
        return mNormalPool;
    }

}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。