android自定义线程池

使用环境:

       开启子线程的时候

使用方法:

 

import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by sensyang.
 * on 2018/10/9 0009.
 */

public class BHThreadPool {

    ThreadPoolExecutor threadPoolExecutor;
    int corePoolSize;
    int maximumPollSize;
    long keepAliveTime;
    public static BHThreadPool instance;

    public static BHThreadPool getInstance() {
        if (instance == null) {
            instance = new BHThreadPool(1,1,1000);
        }
        return instance;
    }

    public BHThreadPool(int corePoolSize, int maximumPollSize, long keepAliveTime){
        this.corePoolSize = corePoolSize;
        this.maximumPollSize = maximumPollSize;
        this.keepAliveTime = keepAliveTime;
    }

    public ThreadPoolExecutor initExecutor(){
        if (threadPoolExecutor == null) {
            synchronized (BHThreadPool.class){
                TimeUnit unit =  TimeUnit.MILLISECONDS;
                ThreadFactory threadFactory = Executors.defaultThreadFactory();
                RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
                LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();

                threadPoolExecutor = new ThreadPoolExecutor(
                        //核心线程数
                        corePoolSize,
                        //最大线程数
                        maximumPollSize,
                        //保持时间
                        keepAliveTime,
                        //保持时间对应的单位
                        unit,
                        workQueue,
                        threadFactory,
                        handler
                );
            }
        }
        return threadPoolExecutor;
    }

    /**
     * 执行任务
     */
    public void executeTask(Runnable runnable){
        initExecutor();
        threadPoolExecutor.execute(runnable);
    }

    /**
     * 提交任务
     */
    public Future<?> commitTask(Runnable runnable){
        initExecutor();
        return threadPoolExecutor.submit(runnable);
    }

    /**
     * 删除任务
     * removeTask()方法起作用有一个必要的前提,就是这个任务还没有开始执行,
     * 如果已经开始执行了,就停止不了该任务了,这个方法就不会起作用
     */
    public void removeTask(Runnable runnable){
        initExecutor();
        threadPoolExecutor.remove(runnable);
    }

    /**
     * 关闭线程池操作的方法
     */
    public void closeThread(){
        threadPoolExecutor.shutdownNow();
    }

    /**
     * 线程池用法
     * 1 BHThreadPool 线程池对象
     * 2 runnable
     * 3 将runnable添加到BHThreadPool线程池中去
     */

    //    BHThreadPool BHThreadPool = new BHThreadPool(1,1,1000);
    //    Runnable runnable = new Runnable() {
    //        @Override
    //        public void run() {
    //    可执行发动handler操作
    //            Log.e("log", "子线程操作");
    //        }
    //    };
    //
    //    BHThreadPool.executeTask(runnable);



}

线程池常用四种,怎么用看自己的选择,这里只是做一个记录罢了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值