一个实用的线程辅助类

另一篇文章 使用Ntrip协议连接CORS服务器获取差分数据-Java 中使用了这个线程辅助类,有人问到,这里把它放上来方便大家参考。

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 线程辅助类
 */
public class ThreadUtils {
    private static final String TAG = ThreadUtils.class.getSimpleName();
    private static final Handler HANDLER = new Handler(Looper.getMainLooper());
    private static final String DEFAULT_PREFIX_OF_TYPE_SINGLE = "Single";
    private static final String DEFAULT_PREFIX_OF_TYPE_CACHED = "Cache";
    private static final String DEFAULT_PREFIX_OF_TYPE_IO = "Io";
    private static final String DEFAULT_PREFIX_OF_TYPE_CPU = "Cpu";
    private static final String DEFAULT_PREFIX_OF_TYPE_SCHEDULE = "Schedule";
    private static final String DEFAULT_PREFIX_OF_TYPE_FIXED = "Fixed";

    private static final byte TYPE_SINGLE = -1;
    private static final byte TYPE_CACHED = -2;
    private static final byte TYPE_IO = -4;
    private static final byte TYPE_CPU = -8;
    private static final byte TYPE_SCHEDULE = -12;

    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();

    /**
     * @return TYPE_SINGLE
     */
    public static byte getTypeSingle() {
        return TYPE_SINGLE;
    }

    /**
     * @return TYPE_CACHED
     */
    public static byte getTypeCached() {
        return TYPE_CACHED;
    }

    /**
     * @return TYPE_IO
     */
    public static byte getTypeIo() {
        return TYPE_IO;
    }

    /**
     * @return TYPE_CPU
     */
    public static byte getTypeCpu() {
        return TYPE_CPU;
    }

    /**
     * @return TYPE_SCHEDULE
     */
    public static byte getTypeSchedule() {
        return TYPE_SCHEDULE;
    }

    /**
     * @return CPU_COUNT
     */
    public static int getCpuCount() {
        return CPU_COUNT;
    }

    /**
     * Return whether the thread is the main thread.
     *
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean isMainThread() {
        return Looper.myLooper() == Looper.getMainLooper();
    }

    /**
     * 获取Handler
     *
     * @return Handler
     */
    public static Handler getMainHandler() {
        return HANDLER;
    }

    /**
     * 在主线程运行
     *
     * @param runnable Runnable
     */
    public static void runOnUiThread(final Runnable runnable) {
        if (runnable == null) {
            return;
        }
        if (isMainThread()) {
            runnable.run();
        } else {
            HANDLER.post(runnable);
        }
    }

    /**
     * 延迟运行
     *
     * @param runnable Runnable
     * @param delay    延迟时间
     * @param timeUnit 时间单位
     */
    public static void runOnUiThreadDelayed(final Runnable runnable, long delay, TimeUnit timeUnit) {
        if (runnable == null) {
            return;
        }
        HANDLER.postDelayed(runnable, timeUnit.toMillis(delay));
    }

    /**
     * 移除任务
     *
     * @param runnable Runnable
     */
    public static void removeCallbacks(Runnable runnable) {
        if (runnable == null) {
            return;
        }
        HANDLER.removeCallbacks(runnable);
    }

    /**
     * Return a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue, using the provided
     * ThreadFactory to create new threads when needed.
     *
     * @param size The size of thread in the pool.
     * @return a fixed thread pool
     */
    public static ExecutorService getFixedPool(@IntRange(from = 1) final int size) {
        return getFixedPool("", size);
    }

    /**
     * Return a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue, using the provided
     * ThreadFactory to create new threads when needed.
     *
     * @param tag  TAG
     * @param size The size of thread in the pool.
     * @return a fixed thread pool
     */
    public static ExecutorService getFixedPool(String tag, @IntRange(from = 1) final int size) {
        return getPoolByTypeAndPriority(tag, size);
    }

    /**
     * Return a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue, using the provided
     * ThreadFactory to create new threads when needed.
     *
     * @param size     The size of thread in the pool.
     * @param priority The priority of thread in the poll.
     * @return a fixed thread pool
     */
    public static ExecutorService getFixedPool(@IntRange(from = 1) final int size,
                                               @IntRange(from = 1, to = 10) final int priority) {
        return getFixedPool("", size, priority);
    }

    /**
     * Return a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue, using the provided
     * ThreadFactory to create new threads when needed.
     *
     * @param tag      TAG
     * @param size     The size of thread in the pool.
     * @param priority The priority of thread in the poll.
     * @return a fixed thread pool
     */
    public static ExecutorService getFixedPool(String tag, @IntRange(from = 1) final int size,
                                               @IntRange(from = 1, to = 10) final int priority) {
        return getPoolByTypeAndPriority(tag, size, priority);
    }

    /**
     * Return a thread pool that uses a single worker thread operating
     * off an unbounded queue, and uses the provided ThreadFactory to
     * create a new thread when needed.
     *
     * @return a single thread pool
     */
    public static ExecutorService getSinglePool() {
        return getSinglePool("");
    }

    /**
     * Return a thread pool that uses a single worker thread operating
     * off an unbounded queue, and uses the provided ThreadFactory to
     * create a new thread when needed.
     *
     * @param tag TAG
     * @return a single thread pool
     */
    public static ExecutorService getSinglePool(String tag) {
        return getPoolByTypeAndPriority(tag, TYPE_SINGLE);
    }

    /**
     * Return a thread pool that uses a single worker thread operating
     * off an unbounded queue, and uses the provided ThreadFactory to
     * create a new thread when needed.
     *
     * @param priority The priority of thread in the poll.
     * @return a single thread pool
     */
    public static ExecutorService getSinglePool(@IntRange(from = 1, to = 10) final int priority) {
        return getSinglePool("", priority);
    }

    /**
     * Return a thread pool that uses a single worker thread operating
     * off an unbounded queue, and uses the provided ThreadFactory to
     * create a new thread when needed.
     *
     * @param tag      TAG
     * @param priority The priority of thread in the poll.
     * @return a single thread pool
     */
    public static ExecutorService getSinglePool(String tag, @IntRange(from = 1, to = 10) final int priority) {
        return getPoolByTypeAndPriority(tag, TYPE_SINGLE, priority);
    }

    /**
     * Return a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available.
     *
     * @return a cached thread pool
     */
    public static ExecutorService getCachedPool() {
        return getCachedPool("");
    }

    /**
     * Return a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available.
     *
     * @param tag TAG
     * @return a cached thread pool
     */
    public static ExecutorService getCachedPool(String tag) {
        return getPoolByTypeAndPriority(tag, TYPE_CACHED);
    }

    /**
     * Return a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available.
     *
     * @param priority The priority of thread in the poll.
     * @return a cached thread pool
     */
    public static ExecutorService getCachedPool(@IntRange(from = 1, to = 10) final int priority) {
        return getCachedPool("", priority);
    }

    /**
     * Return a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available.
     *
     * @param tag      TAG
     * @param priority The priority of thread in the poll.
     * @return a cached thread pool
     */
    public static ExecutorService getCachedPool(String tag, @IntRange(from = 1, to = 10) final int priority) {
        return getPoolByTypeAndPriority(tag, TYPE_CACHED, priority);
    }

    /**
     * Return a thread pool that creates (2 * CPU_COUNT + 1) threads
     * operating off a queue which size is 128.
     *
     * @return a IO thread pool
     */
    public static ExecutorService getIoPool() {
        return getIoPool("");
    }

    /**
     * Return a thread pool that creates (2 * CPU_COUNT + 1) threads
     * operating off a queue which size is 128.
     *
     * @param tag TAG
     * @return a IO thread pool
     */
    public static ExecutorService getIoPool(String tag) {
        return getPoolByTypeAndPriority(tag, TYPE_IO);
    }

    /**
     * Return a thread pool that creates (2 * CPU_COUNT + 1) threads
     * operating off a queue which size is 128.
     *
     * @param priority The priority of thread in the poll.
     * @return a IO thread pool
     */
    public static ExecutorService getIoPool(@IntRange(from = 1, to = 10) final int priority) {
        return getIoPool("", priority);
    }

    /**
     * Return a thread pool that creates (2 * CPU_COUNT + 1) threads
     * operating off a queue which size is 128.
     *
     * @param tag      TAG
     * @param priority The priority of thread in the poll.
     * @return a IO thread pool
     */
    public static ExecutorService getIoPool(String tag, @IntRange(from = 1, to = 10) final int priority) {
        return getPoolByTypeAndPriority(tag, TYPE_IO, priority);
    }

    /**
     * Return a thread pool that creates (CPU_COUNT + 1) threads
     * operating off a queue which size is 128 and the maximum
     * number of threads equals (2 * CPU_COUNT + 1).
     *
     * @return a cpu thread pool for
     */
    public static ExecutorService getCpuPool() {
        return getCpuPool("");
    }

    /**
     * Return a thread pool that creates (CPU_COUNT + 1) threads
     * operating off a queue which size is 128 and the maximum
     * number of threads equals (2 * CPU_COUNT + 1).
     *
     * @param tag TAG
     * @return a cpu thread pool for
     */
    public static ExecutorService getCpuPool(String tag) {
        return getPoolByTypeAndPriority(tag, TYPE_CPU);
    }

    /**
     * Return a thread pool that creates (CPU_COUNT + 1) threads
     * operating off a queue which size is 128 and the maximum
     * number of threads equals (2 * CPU_COUNT + 1).
     *
     * @param priority The priority of thread in the poll.
     * @return a cpu thread pool for
     */
    public static ExecutorService getCpuPool(@IntRange(from = 1, to = 10) final int priority) {
        return getCpuPool("", priority);
    }

    /**
     * Return a thread pool that creates (CPU_COUNT + 1) threads
     * operating off a queue which size is 128 and the maximum
     * number of threads equals (2 * CPU_COUNT + 1).
     *
     * @param tag      TAG
     * @param priority The priority of thread in the poll.
     * @return a cpu thread pool for
     */
    public static ExecutorService getCpuPool(String tag, @IntRange(from = 1, to = 10) final int priority) {
        return getPoolByTypeAndPriority(tag, TYPE_CPU, priority);
    }

    /**
     * Return a thread pool that uses a schedule worker thread operating
     * off an unbounded queue, and uses the provided ThreadFactory to
     * create a new thread when needed.
     *
     * @return a schedule thread pool for
     */
    public static ScheduledExecutorService getSchedulePool() {
        return getSchedulePool("");
    }

    /**
     * Return a thread pool that uses a schedule worker thread operating
     * off an unbounded queue, and uses the provided ThreadFactory to
     * create a new thread when needed.
     *
     * @param tag TAG
     * @return a schedule thread pool for
     */
    public static ScheduledExecutorService getSchedulePool(String tag) {
        return (ScheduledExecutorService) getPoolByTypeAndPriority(tag, TYPE_SCHEDULE);
    }

    /**
     * Return a thread pool that uses a schedule worker thread operating
     * off an unbounded queue, and uses the provided ThreadFactory to
     * create a new thread when needed.
     *
     * @param priority The priority of thread in the poll.
     * @return a schedule thread pool for
     */
    public static ScheduledExecutorService getSchedulePool(@IntRange(from = 1, to = 10) final int priority) {
        return getSchedulePool("", priority);
    }

    /**
     * Return a thread pool that uses a schedule worker thread operating
     * off an unbounded queue, and uses the provided ThreadFactory to
     * create a new thread when needed.
     *
     * @param tag      TAG
     * @param priority The priority of thread in the poll.
     * @return a schedule thread pool for
     */
    public static ScheduledExecutorService getSchedulePool(String tag, @IntRange(from = 1, to = 10) final int priority) {
        return (ScheduledExecutorService) getPoolByTypeAndPriority(tag, TYPE_SCHEDULE, priority);
    }

    /**
     * 获取线程池
     *
     * @param tag  TAG
     * @param type 类型
     * @return 线程池
     */
    public static ExecutorService getPoolByTypeAndPriority(final String tag, final int type) {
        return getPoolByTypeAndPriority(tag, type, Thread.NORM_PRIORITY);
    }

    /**
     * 创建线程池
     *
     * @param tag      TAG
     * @param type     类型
     * @param priority 优先级
     * @return 线程池
     */
    public static ExecutorService getPoolByTypeAndPriority(final String tag, final int type, final int priority) {
        return createPoolByTypeAndPriority(tag, type, priority);
    }

    /**
     * 创建线程池
     *
     * @param tag      TAG
     * @param type     类型
     * @param priority 优先级
     * @return 线程池
     */
    private static ExecutorService createPoolByTypeAndPriority(final String tag, final int type, final int priority) {
        final String prefix;
        if (TextUtils.isEmpty(tag)) {
            switch (type) {
                case TYPE_SINGLE:
                    prefix = DEFAULT_PREFIX_OF_TYPE_SINGLE;
                    break;
                case TYPE_CACHED:
                    prefix = DEFAULT_PREFIX_OF_TYPE_CACHED;
                    break;
                case TYPE_IO:
                    prefix = DEFAULT_PREFIX_OF_TYPE_IO;
                    break;
                case TYPE_CPU:
                    prefix = DEFAULT_PREFIX_OF_TYPE_CPU;
                    break;
                case TYPE_SCHEDULE:
                    prefix = DEFAULT_PREFIX_OF_TYPE_SCHEDULE;
                    break;
                default:
                    prefix = DEFAULT_PREFIX_OF_TYPE_FIXED;
                    break;
            }
        } else {
            switch (type) {
                case TYPE_SINGLE:
                    prefix = DEFAULT_PREFIX_OF_TYPE_SINGLE + "(" + tag + ")";
                    break;
                case TYPE_CACHED:
                    prefix = DEFAULT_PREFIX_OF_TYPE_CACHED + "(" + tag + ")";
                    break;
                case TYPE_IO:
                    prefix = DEFAULT_PREFIX_OF_TYPE_IO + "(" + tag + ")";
                    break;
                case TYPE_CPU:
                    prefix = DEFAULT_PREFIX_OF_TYPE_CPU + "(" + tag + ")";
                    break;
                case TYPE_SCHEDULE:
                    prefix = DEFAULT_PREFIX_OF_TYPE_SCHEDULE + "(" + tag + ")";
                    break;
                default:
                    prefix = DEFAULT_PREFIX_OF_TYPE_FIXED + "(" + tag + ")";
                    break;
            }
        }
        switch (type) {
            case TYPE_SINGLE:
                return Executors.newSingleThreadExecutor(
                        new UtilsThreadFactory(prefix, priority)
                );
            case TYPE_CACHED:
                return Executors.newCachedThreadPool(
                        new UtilsThreadFactory(prefix, priority)
                );
            case TYPE_IO:
                return new ThreadPoolExecutor(2 * CPU_COUNT + 1,
                        2 * CPU_COUNT + 1,
                        30, TimeUnit.SECONDS,
                        new LinkedBlockingQueue<Runnable>(128),
                        new UtilsThreadFactory(prefix, priority)
                );
            case TYPE_CPU:
                return new ThreadPoolExecutor(CPU_COUNT + 1,
                        2 * CPU_COUNT + 1,
                        30, TimeUnit.SECONDS,
                        new LinkedBlockingQueue<Runnable>(128),
                        new UtilsThreadFactory(prefix, priority)
                );
            case TYPE_SCHEDULE:
                return Executors.newScheduledThreadPool(
                        CPU_COUNT + 1,
                        new UtilsThreadFactory(prefix, priority)
                );
            default:
                return Executors.newFixedThreadPool(
                        type,
                        new UtilsThreadFactory(prefix, priority)
                );
        }
    }

    /**
     * 处理线程任务
     *
     * @param executorService 线程池
     * @param type            类型(定时除外)
     * @param runnable        线程
     */
    public static void dealRunnable(ExecutorService executorService, int type, Runnable runnable) {
        dealRunnable("", executorService, type, runnable);
    }

    /**
     * 处理线程任务
     *
     * @param tag             TAG
     * @param executorService 线程池
     * @param type            类型(定时除外)
     * @param runnable        线程
     */
    public static void dealRunnable(String tag, ExecutorService executorService, int type, Runnable runnable) {
        dealRunnable(tag, executorService, type, Thread.NORM_PRIORITY, runnable);
    }

    /**
     * 处理线程任务
     *
     * @param tag             TAG
     * @param executorService 线程池
     * @param type            类型(定时除外)
     * @param priority        优先级
     * @param runnable        线程
     */
    public static void dealRunnable(String tag, ExecutorService executorService, int type, @IntRange(from = 1, to = 10) final int priority, Runnable runnable) {
        if (runnable == null || type == TYPE_SCHEDULE) {
            //定时任务线程池execute方法只执行一次
            return;
        }
        if (executorService == null) {
            switch (type) {
                case TYPE_SINGLE:
                default:
                    executorService = getSinglePool(tag, priority);
                    break;
                case TYPE_CACHED:
                    executorService = getCachedPool(tag, priority);
                    break;
                case TYPE_IO:
                    executorService = getIoPool(tag, priority);
                    break;
                case TYPE_CPU:
                    executorService = getCpuPool(tag, priority);
                    break;
            }
        }
        executorService.execute(runnable);
    }

    /**
     * 处理线程任务
     *
     * @param executorService 线程池
     * @param type            类型(定时除外)
     * @param runnable        线程
     */
    public static Future<?> dealRunnableAndReturn(ExecutorService executorService, int type, Runnable runnable) {
        return dealRunnableAndReturn("", executorService, type, runnable);
    }

    /**
     * 处理线程任务
     *
     * @param executorService 线程池
     * @param type            类型(定时除外)
     * @param runnable        线程
     */
    public static Future<?> dealRunnableAndReturn(String tag, ExecutorService executorService, int type, Runnable runnable) {
        return dealRunnableAndReturn(tag, executorService, type, Thread.NORM_PRIORITY, runnable);
    }

    /**
     * 处理线程任务
     *
     * @param tag             TAG
     * @param executorService 线程池
     * @param type            类型(定时除外)
     * @param priority        优先级
     * @param runnable        线程
     */
    public static Future<?> dealRunnableAndReturn(String tag, ExecutorService executorService, int type, @IntRange(from = 1, to = 10) final int priority, Runnable runnable) {
        if (runnable == null || type == TYPE_SCHEDULE) {
            //定时任务线程池execute方法只执行一次
            return null;
        }
        if (executorService == null) {
            switch (type) {
                case TYPE_SINGLE:
                default:
                    executorService = getSinglePool(tag, priority);
                    break;
                case TYPE_CACHED:
                    executorService = getCachedPool(tag, priority);
                    break;
                case TYPE_IO:
                    executorService = getIoPool(tag, priority);
                    break;
                case TYPE_CPU:
                    executorService = getCpuPool(tag, priority);
                    break;
            }
        }
        return executorService.submit(runnable);
    }

    /**
     * 处理计时器任务
     *
     * @param scheduledExecutorService 计时器线程池
     * @param runnable                 线程
     * @param initialDelay             初始化延迟
     * @param period                   周期
     * @param timeUnit                 时间单位
     */
    public static void dealTimerRunnable(ScheduledExecutorService scheduledExecutorService, Runnable runnable, long initialDelay, long period, TimeUnit timeUnit, boolean atFixedRate) {
        dealTimerRunnable("", scheduledExecutorService, runnable, initialDelay, period, timeUnit, atFixedRate);
    }

    /**
     * 处理计时器任务
     *
     * @param tag                      TAG
     * @param scheduledExecutorService 计时器线程池
     * @param runnable                 线程
     * @param initialDelay             初始化延迟
     * @param period                   周期
     * @param timeUnit                 时间单位
     */
    public static void dealTimerRunnable(String tag, ScheduledExecutorService scheduledExecutorService, Runnable runnable, long initialDelay, long period, TimeUnit timeUnit, boolean atFixedRate) {
        dealTimerRunnable(tag, scheduledExecutorService, Thread.NORM_PRIORITY, runnable, initialDelay, period, timeUnit, atFixedRate);
    }

    /**
     * 处理计时器任务
     *
     * @param tag                      TAG
     * @param scheduledExecutorService 计时器线程池
     * @param runnable                 线程
     * @param initialDelay             初始化延迟
     * @param period                   周期
     * @param timeUnit                 时间单位
     */
    public static void dealTimerRunnable(String tag, ScheduledExecutorService scheduledExecutorService, @IntRange(from = 1, to = 10) final int priority, Runnable runnable, long initialDelay, long period, TimeUnit timeUnit, boolean atFixedRate) {
        if (runnable == null) {
            return;
        }
        if (scheduledExecutorService == null) {
            scheduledExecutorService = getSchedulePool(tag, priority);
        }
        if (atFixedRate) {
            scheduledExecutorService.scheduleAtFixedRate(runnable, initialDelay, period, timeUnit);
        } else {
            scheduledExecutorService.scheduleWithFixedDelay(runnable, initialDelay, period, timeUnit);
        }
    }

    /**
     * 处理计时器任务
     *
     * @param scheduledExecutorService 计时器线程池
     * @param runnable                 线程
     * @param initialDelay             初始化延迟
     * @param period                   周期
     * @param timeUnit                 时间单位
     * @return 任务控制对象
     */
    public static ScheduledFuture<?> dealTimerRunnableAndReturn(ScheduledExecutorService scheduledExecutorService, Runnable runnable, long initialDelay, long period, TimeUnit timeUnit, boolean atFixedRate) {
        return dealTimerRunnableAndReturn("", scheduledExecutorService, runnable, initialDelay, period, timeUnit, atFixedRate);
    }

    /**
     * 处理计时器任务
     *
     * @param tag                      TAG
     * @param scheduledExecutorService 计时器线程池
     * @param runnable                 线程
     * @param initialDelay             初始化延迟
     * @param period                   周期
     * @param timeUnit                 时间单位
     * @return 任务控制对象
     */
    public static ScheduledFuture<?> dealTimerRunnableAndReturn(String tag, ScheduledExecutorService scheduledExecutorService, Runnable runnable, long initialDelay, long period, TimeUnit timeUnit, boolean atFixedRate) {
        return dealTimerRunnableAndReturn(tag, scheduledExecutorService, Thread.NORM_PRIORITY, runnable, initialDelay, period, timeUnit, atFixedRate);
    }

    /**
     * 处理计时器任务
     *
     * @param tag                      TAG
     * @param scheduledExecutorService 计时器线程池
     * @param priority                 优先级
     * @param runnable                 线程
     * @param initialDelay             初始化延迟
     * @param period                   周期
     * @param timeUnit                 时间单位
     * @param atFixedRate              固定频率
     * @return 任务控制对象
     */
    public static ScheduledFuture<?> dealTimerRunnableAndReturn(String tag, ScheduledExecutorService scheduledExecutorService, @IntRange(from = 1, to = 10) final int priority, Runnable runnable, long initialDelay, long period, TimeUnit timeUnit, boolean atFixedRate) {
        if (runnable == null) {
            return null;
        }
        if (scheduledExecutorService == null) {
            scheduledExecutorService = getSchedulePool(tag, priority);
        }
        if (atFixedRate) {
            return scheduledExecutorService.scheduleAtFixedRate(runnable, initialDelay, period, timeUnit);
        } else {
            return scheduledExecutorService.scheduleWithFixedDelay(runnable, initialDelay, period, timeUnit);
        }
    }

    /**
     * 停止线程池
     *
     * @param executorService ExecutorService
     */
    public static void stopExecutorService(ExecutorService executorService) {
        stopExecutorService(executorService, 1, TimeUnit.SECONDS);
    }

    /**
     * 停止线程池
     *
     * @param executorService ExecutorService
     * @param timeOut         TimeOut
     * @param timeUnit        TimeUnit
     */
    public static void stopExecutorService(ExecutorService executorService,
                                           long timeOut, TimeUnit timeUnit) {
        if (executorService != null) {
            executorService.shutdown();
            try {
                if (!executorService.awaitTermination(timeOut, timeUnit)) {
                    executorService.shutdownNow();
                    if (!executorService.awaitTermination(timeOut, timeUnit)) {
                        System.err.println("Pool did not terminate");
                    }
                }
            } catch (InterruptedException e) {
                executorService.shutdownNow();
                Thread.currentThread().interrupt();
//                e.printStackTrace();
            }
        }
    }

    /**
     * UtilsThreadFactory
     */
    private static final class UtilsThreadFactory extends AtomicLong implements ThreadFactory {
        private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);
        private final ThreadGroup group;
        private final String namePrefix;
        private final int priority;

        UtilsThreadFactory(String prefix, int priority) {
            SecurityManager s = System.getSecurityManager();
            group = s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
            namePrefix = prefix + "-pool-" +
                    POOL_NUMBER.getAndIncrement() +
                    "-thread-";
            this.priority = priority;
        }

        @Override
        public Thread newThread(@NonNull Runnable r) {
            Thread t = new Thread(group, r, namePrefix + getAndIncrement(), 0) {
                @Override
                public void run() {
                    try {
                        super.run();
                    } catch (Throwable t) {
                        Log.e(TAG, "Request threw uncaught throwable", t);
                    }
                }
            };
            if (t.isDaemon()) {
                t.setDaemon(false);
            }
            t.setPriority(priority);
            return t;
        }
    }
}
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:设计师小姐姐 返回首页