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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 线程工具类,负责执行某个线程,放在线程池里性价比比价高相关的知识,希望对你有一定的参考价值。

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;
        }
    }
}

以上是关于java 线程工具类,负责执行某个线程,放在线程池里性价比比价高的主要内容,如果未能解决你的问题,请参考以下文章

Java多线程01(Thread类线程创建线程池)

java并发编程线程池的使用

Java线程池基本使用

java线程池源码的理解

Java多线程与并发——线程生命周期和线程池

JAVA线程池之周期性地执行任务