其他分享
首页 > 其他分享> > 线程池使用

线程池使用

作者:互联网

线程池

1.工具类实现

/**
* @title: ThreadUtil
* @Author bao
* @description
* @Date: 2021/12/2216:07
*/
public class ThreadUtil {
   private static volatile ExecutorService executorService;

   //获取单例对象
   public static ExecutorService getInstance(){
       if (executorService == null) {
           synchronized (ThreadPoolManager.class) {
               if (executorService == null) {
                   int cpuNum = Runtime.getRuntime().availableProcessors();// 获取处理器数量
                   int threadNum = cpuNum * 2;// 根据cpu数量,计算出合理的线程并发数
                   executorService = Executors.newFixedThreadPool(threadNum);
              }
          }
      }
       return executorService;
  }


//ceshi
   public static void main(String[] args) {
       ExecutorService instance = ThreadUtil.getInstance();
       long taskCount = 0;
       for (int i = 0; i < 100; i++) {
           int j = i;
           ThreadUtil.getInstance().execute(()->{
               System.out.println(j);
          });

           taskCount = ((ThreadPoolExecutor)instance).getActiveCount();
           System.out.println("taskCount:"+taskCount);
      }
  }

}

线程池监控:

long activeCount = ((ThreadPoolExecutor)instance).getActiveCount();

自定义线程池

工具类

/**
* @title: ThreadUtil
* @Author bao
* @description
* @Date: 2021/12/2215:39
*/
public class ThreadManager {

   public static volatile ThreadPool instance;


   // 获取单例的线程池对象
   public static ThreadPool getInstance() {
       if (instance == null) {
           synchronized (ThreadPoolManager.class) {
               if (instance == null) {
                   int cpuNum = Runtime.getRuntime().availableProcessors();// 获取处理器数量
                   int threadNum = cpuNum * 2;// 根据cpu数量,计算出合理的线程并发数
                   instance = new ThreadPool(threadNum, threadNum+1, Integer.MAX_VALUE);
              }
          }
      }
       return instance;
  }

   public static class ThreadPool {
       private ThreadPoolExecutor mExecutor;
       private int corePoolSize;
       private int maximumPoolSize;
       private long keepAliveTime;

       private ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
           this.corePoolSize = corePoolSize;
           this.maximumPoolSize = maximumPoolSize;
           this.keepAliveTime = keepAliveTime;
      }

       public void execute(Runnable runnable) {
           if (runnable == null) {
               return;
          }
           if (mExecutor == null) {
               mExecutor = new ThreadPoolExecutor(corePoolSize,// 核心线程数
                       maximumPoolSize, // 最大线程数
                       keepAliveTime, // 闲置线程存活时间
                       TimeUnit.MILLISECONDS,// 时间单位
                       new LinkedBlockingDeque<Runnable>(Integer.MAX_VALUE),// 线程队列
                       Executors.defaultThreadFactory(),// 线程工厂
                       new ThreadPoolExecutor.AbortPolicy() {// 队列已满,而且当前线程数已经超过最大线程数时的异常处理策略
                           @Override
                           public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                               super.rejectedExecution(r, e);
                          }
                      }
              );
          }
           mExecutor.execute(runnable);
      }
  }


   public static void main(String[] args) {
       for (int i = 0; i < 100; i++) {
           int j = i;
           ThreadManager.getInstance().execute(()->{
               System.out.println(j);
          });
      }
  }

}

参数

new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);

线程数选择

标签:int,instance,线程,使用,corePoolSize,public,ThreadPoolExecutor
来源: https://www.cnblogs.com/oeds/p/15762968.html