diff --git a/src/main/java/com/cm/population/pojo/dtos/populationinfo/PopulationInfoBaseDTO.java b/src/main/java/com/cm/population/pojo/dtos/populationinfo/PopulationInfoBaseDTO.java index de3c631..879aa3e 100644 --- a/src/main/java/com/cm/population/pojo/dtos/populationinfo/PopulationInfoBaseDTO.java +++ b/src/main/java/com/cm/population/pojo/dtos/populationinfo/PopulationInfoBaseDTO.java @@ -112,12 +112,12 @@ public class PopulationInfoBaseDTO { this.healthStatus = healthStatus; } - public String getIsMinority() { + /*public String getIsMinority() { if (getAge() < 18) { return "是"; } return "否"; - } + }*/ public void setIsMinority(String isMinority) { this.isMinority = isMinority; @@ -134,14 +134,14 @@ public class PopulationInfoBaseDTO { this.isSeriousIllness = isSeriousIllness; } - public Integer getAge() { + /*public Integer getAge() { if (StringUtils.isBlank(this.getBirthday())) { return 0; } LocalDate birthDate = LocalDate.parse(this.birthday); Period period = Period.between(birthDate, LocalDate.now()); return period.getYears(); - } + }*/ public void setAge(Integer age) { this.age = age; diff --git a/src/main/java/com/cm/population/service/population/impl/PopulationServiceImpl.java b/src/main/java/com/cm/population/service/population/impl/PopulationServiceImpl.java index d9c165c..a015e91 100644 --- a/src/main/java/com/cm/population/service/population/impl/PopulationServiceImpl.java +++ b/src/main/java/com/cm/population/service/population/impl/PopulationServiceImpl.java @@ -21,6 +21,7 @@ import com.cm.population.service.population.IPopulationService; import com.cm.population.service.populationinfo.IPopulationInfoService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import com.google.common.collect.Lists; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -470,27 +471,40 @@ public class PopulationServiceImpl extends AbstractService implements IPopulatio int size = populationInfoIds.size(); int numThreads = size / 2 + (size % 2 == 0 ? 0 : 1); // 计算需要的线程数 ExecutorService executorService = Executors.newFixedThreadPool(numThreads); + List>> futures = new ArrayList<>(); - Callable> task = () -> { - List output = new ArrayList<>(); - for (String str : populationInfoIds) { - PopulationInfoBaseDTO base = populationInfoService.getBase(str); - output.add(base); - } - return output; - }; + // 将populationInfoIds分割成多个子列表,每个子列表由一个任务处理 + int chunkSize = (int) Math.ceil((double) populationInfoIds.size() / numThreads); + List> chunks = Lists.partition(populationInfoIds, chunkSize); - Future> futureResult = executorService.submit(task); - try { - List result = futureResult.get(); - populationInfoBaseDTOs.addAll(result); - executorService.shutdown(); - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); + for (List chunk : chunks) { + Callable> task = () -> { + List output = new ArrayList<>(); + for (String str : chunk) { + try { + PopulationInfoBaseDTO base = populationInfoService.getBase(str); + output.add(base); + } catch (Exception e) { + // 处理或记录异常 + e.printStackTrace(); + } + } + return output; + }; + futures.add(executorService.submit(task)); } + // 等待所有任务完成并合并结果 + for (Future> future : futures) { + try { + populationInfoBaseDTOs.addAll(future.get()); + } catch (InterruptedException | ExecutionException e) { + // 处理或记录异常 + e.printStackTrace(); + } + } + executorService.shutdown(); // 关闭线程池 + Map houseMap = null; Map baseMap = null;