Android Thread线程池管理类

本文介绍了一个基于Java实现的线程池管理类,详细展示了如何使用LinkedBlockingQueue和ThreadPoolExecutor来创建和管理线程池,包括任务的添加、拒绝策略的设定以及线程的执行流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.yunduan.parking.manager;

/**
 * author cowards
 * created on 2018\10\17 0017
 **/

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

/**
 * 1.  线程池管理类
 */
public class ThreadPoolManager {
    //1.将请求任务放到请求队列中
    //通过阻塞式队列来存储任务
    private LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();

    //2.把队列中的任务放到线程池
    private ThreadPoolExecutor threadPoolExecutor;

    private LinkedBlockingQueue<Runnable> removeTask = new LinkedBlockingQueue<>();

    //单例模式
    private static ThreadPoolManager singleInstance = new ThreadPoolManager();

    public static ThreadPoolManager getSingleInstance() {
        return singleInstance;
    }

    //私有化的构造方法
    private ThreadPoolManager() {
        threadPoolExecutor = new ThreadPoolExecutor(4, 20, 15, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(4), rejectedExecutionHandler);
        //运行线程池
        threadPoolExecutor.execute(runnable);
    }

    //添加任务
    public void execute(Runnable runnable) {
        if (runnable != null) {
            try {
                queue.put(runnable);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //当线程数超过maxPoolSize或者keep-alive时间超时时执行拒绝策略
    private RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler() {
        /**
         * @param runnable 超时被线程池抛弃的线程
         * @param threadPoolExecutor
         */
        @Override
        public void rejectedExecution(Runnable runnable, ThreadPoolExecutor threadPoolExecutor) {
            //将该线程重新放入请求队列中
            try {
                queue.put(runnable);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    };
    //3.让他们开始工作起来
    //整个的工作线程
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while (true) {
                Runnable runnable = null;
                //不断从从请求队列中取出请求
                try {
                    runnable = queue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //如果不为空,放入线程池中执行
                if (runnable != null) {
                    if (!removeTask.contains(runnable)) {
                        threadPoolExecutor.execute(runnable);
                    }
                }
            }
        }
    };

    public void remove(Runnable runnable) {
        try {
            removeTask.put(runnable);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void reset() {
        removeTask.clear();
        queue.clear();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落魄的Android开发

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值