Java新建一个子线程异步运行方法

打印 上一主题 下一主题

主题 912|帖子 912|积分 2736

如何在运行主方法的同时异步运行另一个方法,我是用来更新缓存;
1. 工具类
  1. public class ThreadPoolUtils {
  2.     private static final Logger LOGGER = LoggerFactory.getLogger(ThreadPoolUtils.class);
  3.     private static final String POOL_NAME = "thread-im-runner";
  4.     // 等待队列长度
  5.     private static final int BLOCKING_QUEUE_LENGTH = 20000;
  6.     // 闲置线程存活时间
  7.     private static final int KEEP_ALIVE_TIME = 5 * 1000;
  8.     private static ThreadPoolExecutor threadPool = null;
  9.     private ThreadPoolUtils() {
  10.         throw new IllegalStateException("utility class");
  11.     }
  12.     /**
  13.      * 无返回值直接执行
  14.      *
  15.      * @param runnable 需要运行的任务
  16.      */
  17.     public static void execute(Runnable runnable) {
  18.         getThreadPool().execute(runnable);
  19.     }
  20.     /**
  21.      * 有返回值执行 主线程中使用Future.get()获取返回值时,会阻塞主线程,直到任务执行完毕
  22.      *
  23.      * @param callable 需要运行的任务
  24.      */
  25.     public static <T> Future<T> submit(Callable<T> callable) {
  26.         return getThreadPool().submit(callable);
  27.     }
  28.     private static synchronized ThreadPoolExecutor getThreadPool() {
  29.         if (threadPool == null) {
  30.             // 核心线程数、最大线程数、闲置线程存活时间、时间单位、线程队列、线程工厂、当前线程数已经超过最大线程数时的异常处理策略
  31.             threadPool = new ThreadPoolExecutor(50, 500, KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
  32.                 new ArrayBlockingQueue<>(BLOCKING_QUEUE_LENGTH),
  33.                 new ThreadFactoryBuilder().setNameFormat(POOL_NAME + "-%d").build(),
  34.                 new ThreadPoolExecutor.AbortPolicy() {
  35.                     @Override
  36.                     public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
  37.                         LOGGER.warn("线程过多,当前运行线程总数:{},活动线程数:{}。等待队列已满,等待运行任务数:{}", e.getPoolSize(), e.getActiveCount(),
  38.                             e.getQueue().size());
  39.                     }
  40.                 });
  41.         }
  42.         return threadPool;
  43.     }
  44.     private static synchronized ThreadPoolExecutor getThreadPoolByCpuNum() {
  45.         if (threadPool == null) {
  46.             // 获取处理器数量
  47.             int cpuNum = Runtime.getRuntime().availableProcessors();
  48.             // 根据cpu数量,计算出合理的线程并发数
  49.             int maximumPoolSize = cpuNum * 2 + 1;
  50.             // 核心线程数、最大线程数、闲置线程存活时间、时间单位、线程队列、线程工厂、当前线程数已经超过最大线程数时的异常处理策略
  51.             threadPool = new ThreadPoolExecutor(maximumPoolSize - 1, maximumPoolSize, KEEP_ALIVE_TIME,
  52.                 TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(BLOCKING_QUEUE_LENGTH),
  53.                 new ThreadFactoryBuilder().setNameFormat(POOL_NAME + "-%d").build(),
  54.                 new ThreadPoolExecutor.AbortPolicy() {
  55.                     @Override
  56.                     public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
  57.                         LOGGER.warn("线程爆炸了,当前运行线程总数:{},活动线程数:{}。等待队列已满,等待运行任务数:{}", e.getPoolSize(), e.getActiveCount(),
  58.                             e.getQueue().size());
  59.                     }
  60.                 });
  61.         }
  62.         return threadPool;
  63.     }
  64. }
复制代码
2.实际使用
  1.     ThreadPoolUtils.execute(() -> {
  2.             this.Method();
  3.         });
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

三尺非寒

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表