executor服务被卡住了,没有抛出错误

7ajki6be  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(379)

当我递归调用下面的方法时,它会被阻塞,不会抛出错误。没有回应。线程池大小为50。在处理10万条记录时,我面临这个问题。如何解决这个问题?提前谢谢

public void processActivityDeposits(
    Activity activity) {
  int partitionSize =
      environment.getProperty("patition.size", Integer.class, 1000);
  List<Long> records =
      service.findReadyToProcessRecords(
          activity.getActivityId(),
          partitionSize);
  if (records != null && records.size() > 0) {
    try {
      int threadPoolSize =
          environment.getProperty("thread.pool.size", Integer.class, 10);
      ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);
      for (Long activityId : records) {
        executorService.execute(
            new ActivityDepositInternalProcessor(
                activityId));
      }
      executorService.shutdown();
      try {
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      processActivityDeposits(
          activity);
    } catch (Exception exception) {
      exception.printStackTrace();
    }
  }
}
ktca8awb

ktca8awb1#

我建议您对代码进行以下更改。答。将执行服务创建和关闭移出if块。
b。不要将线程池大小设置为5以上,因为您只有一个核心,

public void processActivityDeposits(
    Activity activity) {
  int partitionSize =
      environment.getProperty("patition.size", Integer.class, 1000);
  List<Long> records =
      service.findReadyToProcessRecords(
          activity.getActivityId(),
          partitionSize);
        //moved out of if block
       int threadPoolSize =
          environment.getProperty("thread.pool.size", Integer.class, 10);
      ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);     
  if (records != null && records.size() > 0) {
    try {

      for (Long activityId : records) {
        executorService.execute(
            new ActivityDepositInternalProcessor(
                activityId));
      }

      processActivityDeposits(
          activity);
    } catch (Exception exception) {
      exception.printStackTrace();
    }
  }
  //moved out of if block
   executorService.shutdown();
      try {
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
}

相关问题