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

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

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

  1. public void processActivityDeposits(
  2. Activity activity) {
  3. int partitionSize =
  4. environment.getProperty("patition.size", Integer.class, 1000);
  5. List<Long> records =
  6. service.findReadyToProcessRecords(
  7. activity.getActivityId(),
  8. partitionSize);
  9. if (records != null && records.size() > 0) {
  10. try {
  11. int threadPoolSize =
  12. environment.getProperty("thread.pool.size", Integer.class, 10);
  13. ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);
  14. for (Long activityId : records) {
  15. executorService.execute(
  16. new ActivityDepositInternalProcessor(
  17. activityId));
  18. }
  19. executorService.shutdown();
  20. try {
  21. executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. processActivityDeposits(
  26. activity);
  27. } catch (Exception exception) {
  28. exception.printStackTrace();
  29. }
  30. }
  31. }
ktca8awb

ktca8awb1#

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

  1. public void processActivityDeposits(
  2. Activity activity) {
  3. int partitionSize =
  4. environment.getProperty("patition.size", Integer.class, 1000);
  5. List<Long> records =
  6. service.findReadyToProcessRecords(
  7. activity.getActivityId(),
  8. partitionSize);
  9. //moved out of if block
  10. int threadPoolSize =
  11. environment.getProperty("thread.pool.size", Integer.class, 10);
  12. ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);
  13. if (records != null && records.size() > 0) {
  14. try {
  15. for (Long activityId : records) {
  16. executorService.execute(
  17. new ActivityDepositInternalProcessor(
  18. activityId));
  19. }
  20. processActivityDeposits(
  21. activity);
  22. } catch (Exception exception) {
  23. exception.printStackTrace();
  24. }
  25. }
  26. //moved out of if block
  27. executorService.shutdown();
  28. try {
  29. executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  30. } catch (InterruptedException e) {
  31. e.printStackTrace();
  32. }
  33. }
展开查看全部

相关问题