写在前边:
最近接手的老项目线程的使用都是在代码中显示创建,这样对内存开销很大,并且容易报错,故而使用了以下线程池的操作进行替换,这也是大佬们常用的封装方法。使用方法暴力简单,如下所示:

完整工具类:

package com.refly.pigcommissioner.threadutil;

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

/**
 * creat by tianfu.yang 2018/04/24
 * @author ytf
 */

public class ThreadPoolUtil {
    //线程池核心线程数
    private static int CORE_POOL_SIZE = 5;
    //线程池最大线程数
    private static int MAX_POOL_SIZE = 100;
    //额外线程空状态生存时间
    private static int KEEP_ALIVE_TIME = 10000;
    //阻塞队列。当核心线程都被占用,且阻塞队列已满的情况下,才会开启额外线程。
    private static BlockingQueue workQueue = new ArrayBlockingQueue(10);
    //线程池
    private static ThreadPoolExecutor threadPool;

    private ThreadPoolUtil() {
    }

    //线程工厂
    private static ThreadFactory threadFactory = new ThreadFactory() {
        private final AtomicInteger integer = new AtomicInteger();

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "myThreadPool thread:" + integer.getAndIncrement());
        }
    };

    static {
        threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME,
                TimeUnit.SECONDS, workQueue, threadFactory);
    }

    public static void execute(Runnable runnable) {
        threadPool.execute(runnable);
    }

    public static void execute(FutureTask futureTask) {
        threadPool.execute(futureTask);
    }

    public static void cancel(FutureTask futureTask) {
        futureTask.cancel(true);
    }
}

使用线程池操作方法:

    void getLocationSetting(){
        try {
            ThreadPoolUtil.execute(new Runnable() {
                @Override
                public void run() {
                    locationSettingResult = netHandler.postSelectLocationSetting(uSP.userId().get());
                    if (locationSettingResult!=null&& "c0".equals(locationSettingResult.getCode())) {
                        int timesPerHour = locationSettingResult.getBody().getTimesPerHour();
                        //每小时定们次数
                        uSP.sendTime().put(timesPerHour);
                        String startTime = locationSettingResult.getBody().getStartTime();
                        uSP.locationStartTime().put(startTime);
                        String endTime = locationSettingResult.getBody().getEndTime();
                        uSP.locationEndTime().put(endTime);
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            com.orhanobut.logger.Logger.e("获取定位设置信息"+e.toString());
        }
    }

未使用线程池操作方法:

    void getLocationSetting(){
        try {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    locationSettingResult = netHandler.postSelectLocationSetting(uSP.userId().get());
//                    if (locationSettingResult!=null&& "c0".equals(locationSettingResult.getCode())) {
//                        int timesPerHour = locationSettingResult.getBody().getTimesPerHour();
//                        uSP.sendTime().put(timesPerHour); //每小时定们次数
//                        String startTime = locationSettingResult.getBody().getStartTime();
//                        uSP.locationStartTime().put(startTime);
//                        String endTime = locationSettingResult.getBody().getEndTime();
//                        uSP.locationEndTime().put(endTime);
//                    }
//                }
//            }).start();
        } catch (Exception e) {
            e.printStackTrace();
            com.orhanobut.logger.Logger.e("获取定位设置信息"+e.toString());
        }
    }
GitHub 加速计划 / th / ThreadPool
7.74 K
2.22 K
下载
A simple C++11 Thread Pool implementation
最近提交(Master分支:2 个月前 )
9a42ec13 - 9 年前
fcc91415 - 9 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐