The use of ThreadPoolUtil, a custom thread pool tool developed by Android

Keywords: Java

In the front:
Recently, the threads of the old projects are created in the code, which costs a lot of memory and is easy to report errors. Therefore, the following thread pool operations are used for replacement, which is also a common encapsulation method used by the tycoons. The use of violence is simple, as follows:

Complete tool class:

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 {
    //Number of core threads in thread pool
    private static int CORE_POOL_SIZE = 5;
    //Maximum threads in thread pool
    private static int MAX_POOL_SIZE = 100;
    //Extra thread empty state lifetime
    private static int KEEP_ALIVE_TIME = 10000;
    //Block the queue. When the core threads are occupied and the blocking queue is full, additional threads will be started.
    private static BlockingQueue workQueue = new ArrayBlockingQueue(10);
    //Thread pool
    private static ThreadPoolExecutor threadPool;

    private ThreadPoolUtil() {
    }

    //Thread factory
    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);
    }
}

Use thread pool operation method:

    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();
                        //Number of people per hour
                        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("Get location setting information"+e.toString());
        }
    }

Unused thread pool operation method:

    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); //Number of people per hour
//                        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("Get location setting information"+e.toString());
        }
    }

Posted by cerebrus189 on Fri, 03 Apr 2020 09:43:07 -0700