监控ThreadPoolExecutor具体Task在Queue中等待时间

可监控_ThreadPoolExecutor

  • 主要关心的问题:

    * 最关心的监控数据:
    * 1.task队列中等待时间
    * 2.task任务执行时间
    *
    *  两种方案:
    * 1. future_task
    * 2. queue
    * 注意:Config-自动获取配置文件注解(后面有时间写)
    
  • 处理:

    • 继承,初始化构造方法

      private static MonitoringLinkedBlockingQueue monitoringQueue = new MonitoringLinkedBlockingQueue(maxQueueSize);
      
      public MonitoringThreadPoolExecutor() {
          super(CPU_PROCESSOR_NUM << 1, CPU_PROCESSOR_NUM << 2, 50, TimeUnit.SECONDS,
                  monitoringQueue,
                  new NamedThreadFactory(THREAD_FACTORY_NAME, false), new RejectedExecutionHandler() {
                      @Override
                      public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                          Monitor.recordOne("monitoring_thread_poll_task_reject");
                      }
                  });
          //设置为true,corePoolSize_alivetime后自动销毁
          allowCoreThreadTimeOut(true);
      }
    • init-基本监控定时获取

      @PostConstruct
      private void init() {
      //添加常用监控
      final ScheduledExecutorService coreMonitorService = Executors.newScheduledThreadPool(1);
      coreMonitorService.scheduleAtFixedRate(new Runnable() {
          @Override
          public void run() {
              Monitor.recordSize(THREAD_FACTORY_NAME + "_active_thread_count", getActiveCount());
              Monitor.recordSize(THREAD_FACTORY_NAME + "_pool_size", getPoolSize());
              Monitor.recordSize(THREAD_FACTORY_NAME + "_queue_size", getQueue().size());
              Monitor.recordSize(THREAD_FACTORY_NAME + "_largest_pool_size", getLargestPoolSize());
              Monitor.recordSize(THREAD_FACTORY_NAME + "_task_count", getTaskCount());
              Monitor.recordSize(THREAD_FACTORY_NAME + "_completed_task_count", getCompletedTaskCount());
          }
      }, 1L, 2L, TimeUnit.SECONDS);
      
      Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
          @Override
          public void run() {
              shutdown();
              coreMonitorService.shutdown();
          }
      }));
      }
    • 重写submit

      @Override
      public MonitoringFutureTask submit(Runnable task){
      if (task == null) {
          throw new NullPointerException();
      }
      if (task instanceof NamedRunnable) {
          MonitoringFutureTask monitoringFutureTask = new MonitoringFutureTask<>(task, null);
          execute(monitoringFutureTask);
          return monitoringFutureTask;
      }else {
          throw new ForbiddenException("请使用NamedRunnable!");
      }
      }
    • 动态修改线程池核心配置

      @Config("can_monitor_thread_pool_executor.properties")
      private void changeProperties(Map config) {
      logger.info("can_monitor_thread_pool_executor:{}", config);
      if (config != null) {
          int coreSize = IntegerUtil.parseStrToInt(config.get("coreSize"));
          if (coreSize > 0) {
              setCorePoolSize(coreSize);
          }
          int maxSize = IntegerUtil.parseStrToInt(config.get("maxSize"));
          if (maxSize > 0) {
              setMaximumPoolSize(maxSize);
          }
          int queueSize = IntegerUtil.parseStrToInt(config.get("queueSize"));
          if (queueSize > 0) {
              monitoringQueue.setMaxQueueSize(queueSize);
          }
          int keepAliveTime = IntegerUtil.parseStrToInt(config.get("keepAliveTime"));
          if (keepAliveTime > 0) {
              String timeUnitStr = config.get("timeUnit");
              if (timeUnitStr != null) {
                  TimeUnit timeUnit;
                  try {
                      timeUnit = TimeUnit.valueOf(timeUnitStr);
                      setKeepAliveTime(keepAliveTime, timeUnit);
                  } catch (IllegalArgumentException e) {
                      Monitor.recordOne("timeUnit_config_error");
                      setKeepAliveTime(keepAliveTime, TimeUnit.SECONDS);
                  }
              } else {
                  setKeepAliveTime(keepAliveTime, TimeUnit.SECONDS);
              }
          }
      }
      }
    • -

你可能感兴趣的:(多线程)