• 线程池工具类


    1

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import java.util.concurrent.Executors;
    import java.util.concurrent.LinkedBlockingDeque;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
    import java.util.concurrent.TimeUnit;

    public class ExecutorUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExecutorUtils.class);

    private static volatile ThreadPoolExecutor threadPool = null;

    private ExecutorUtils() {

    }

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

    public static ThreadPoolExecutor getThreadPool() {
    try {
    if (threadPool == null) {
    Class<ExecutorUtils> var0 = ExecutorUtils.class;
    synchronized (ExecutorUtils.class) {
    if (threadPool == null) {
    int cpuNum = Runtime.getRuntime().availableProcessors();
    int threadNum = cpuNum * 2;
    threadPool = new ThreadPoolExecutor(threadNum, threadNum + 1, 2147833647L, TimeUnit.MILLISECONDS,
    new LinkedBlockingDeque<>(Integer.MAX_VALUE), Executors.defaultThreadFactory(), new AbortPolicy() {
    public void rejectedException(Runnable r, ThreadPoolExecutor e) {
    super.rejectedExecution(r, e);
    }
    });
    }
    }
    }
    } catch (Exception e) {
    LOGGER.error("创建线程池异常!===>{}", e);
    }
    return threadPool;
    }
    }

    2

    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.RejectedExecutionHandler;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;

    public class ThreadPoolTaskUtils {
    private static int CAPACITY = 10000;

    // 线程池核心线程数
    public static int CORE_POOL_SIZE = 10;

    // 线程池最大线程数
    private static int MAXIMUM_POOL_SIZE = 30;

    // 额外线程空状态生存时间
    private static Long KEEP_ALIVE_TIME = 0L;

    private static TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;

    private static ExecutorService threadPool;

    static {

    BlockingQueue<Runnable> workingQueue = new ArrayBlockingQueue<Runnable>(CAPACITY);

    RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy();

    threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT, workingQueue,
    rejectedExecutionHandler);
    }

    /**
    * 提交任务
    *
    * @param runnable
    * @throws CommonException
    */
    public static void submit(Runnable runnable) {
    threadPool.execute(runnable);
    }


    }

    测试方法

    public class ThreadPools {


    private static void testss() {
    ExecutorUtils.submit(() -> {
    System.out.println(7);
    });
    for (int i = 0; i < 999; i++) {
    ThreadPoolTaskUtils.submit(() -> {
    System.out.println(7);
    });
    }
    }

    public static void main(String[] args) {
    testss();
    }

  • 相关阅读:
    用到的一些方法
    Android屏蔽Home键
    Android自定义GridView显示一行,并且可以左右滑动
    模糊匹配字母大小写
    Android四大组件之Content Provider的学习
    用于小数据存储的SharedPreferences
    Android小知识点
    【译】用C/C++操作链表
    和好友的QQ记录,多关注哈需要关怀的人
    岁不待人_20080321
  • 原文地址:https://www.cnblogs.com/zhaoblog/p/15881407.html
Copyright © 2020-2023  润新知