Backlight87
8/24/2017 - 3:26 PM

线程工具类,负责执行某个线程,放在线程池里性价比比价高

线程工具类,负责执行某个线程,放在线程池里性价比比价高

public class ThreadUtils {
    // ===========================================================
    // Fields
    // ===========================================================
    private static ThreadPoolExecutor sThreadPoolExecutor;

    // ===========================================================
    // Define Methods
    // ===========================================================

    /**
     * 执行任务
     * @param task
     */
    public static void execute(Runnable task) {
        if (null != task) {
            ThreadPoolExecutor executor = getDefaultThreadPool();
            if (null != executor) {
                try {
                    executor.execute(task);
                } catch (Exception e) {
                    Log.e("dddd",e.toString());
                }
            }
        }
    }

    /**
     * 移除任务
     * @param task
     */
    public static void removeTask(Runnable task) {
        if (null != task) {
            ThreadPoolExecutor executor = getDefaultThreadPool();
            if (null != executor) {
                try {
                    executor.remove(task);
                    executor.purge();
                } catch (Exception e) {
                   // Dog.e(e);
                }
            }
        }
    }

    /**
     * 获取当前可用的线程池对象
     * @return
     */
    private static ThreadPoolExecutor getDefaultThreadPool() {
        if (null == sThreadPoolExecutor) {
            sThreadPoolExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool(new RenameThreadFactory());
            // 始终存在Ncpu 个线程,除非设置了允许CoreThread超时
            sThreadPoolExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
            // 设置空闲线程允许存活的时间,防止空闲线程过多导致资源浪费
            sThreadPoolExecutor.setKeepAliveTime(10L, TimeUnit.SECONDS);
        }
        return sThreadPoolExecutor;
    }

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================

    private static class RenameThreadFactory implements ThreadFactory {
        private static final AtomicInteger poolNumber = new AtomicInteger(1);
        private final ThreadGroup group;
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        private final String namePrefix;

        public RenameThreadFactory() {
            SecurityManager s = System.getSecurityManager();
            group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
            namePrefix = "ThreadUtils-" + poolNumber.getAndIncrement() + "-thread-";
        }

        public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
            if (t.isDaemon())
                t.setDaemon(false);
            if (t.getPriority() != Thread.NORM_PRIORITY)
                t.setPriority(Thread.NORM_PRIORITY);
            return t;
        }
    }
}