我在使用CompletableFuture时做错了什么,导致它们没有并行执行。

huangapple go评论87阅读模式
英文:

What I'm doing wrong with CompletableFuture that aren't executing in parallel

问题

我已将列表分为5部分,并且想使用restTemplate并行执行。我有以下方法:

  1. @Async
  2. private CompletableFuture<List<Cidade>> MontarCidadesPorEstado(List<Cidade> listaCidades, List<UF> estado) {
  3. estado.forEach(es -> {
  4. Map<String, String> param = new HashMap<String, String>();
  5. param.put("UF", es.getSigla());
  6. Cidade[] cidades = restTemplate.getForObject(url, Cidade[].class, param);
  7. listaCidades.addAll(Arrays.asList(cidades));
  8. });
  9. return CompletableFuture.completedFuture(listaCidades);
  10. }

所以我创建了一个CompletableFuture列表,以便并行执行所有方法。但是,当我将每个CompletableFuture添加到列表中时,它已经开始执行,而不是并行执行,例如:

  1. List<List<UF>> subSets = Lists.partition(estados, 5);
  2. List<CompletableFuture<List<Cidade>>> lstCompletableFuture = new ArrayList();
  3. subSets.forEach(estado -> {
  4. lstCompletableFuture.add(MontarCidadesPorEstado(listaCidades, estado));
  5. });
  6. CompletableFuture.allOf(lstCompletableFuture.toArray(new CompletableFuture[lstCompletableFuture.size()]));

我做错了什么?我以为在调用CompletableFuture.allOf时,应该执行MontarCidadesPorEstado方法。

英文:

I have splitted a list in 5 parts, and I want to execute in parallel using restTemplate, I have the following method:

  1. @Async
  2. private CompletableFuture&lt;List&lt;Cidade&gt;&gt; MontarCidadesPorEstado(List&lt;Cidade&gt; listaCidades, List&lt;UF&gt; estado) {
  3. estado.forEach(es -&gt; {
  4. Map&lt;String, String&gt; param = new HashMap&lt;String, String&gt;();
  5. param.put(&quot;UF&quot;, es.getSigla());
  6. Cidade[] cidades = restTemplate.getForObject(url, Cidade[].class, param);
  7. listaCidades.addAll(Arrays.asList(cidades));
  8. });
  9. return CompletableFuture.completedFuture(listaCidades);
  10. }

So created a List of CompletableFuture to execute all of them in parallel, but when an add each CompletableFuture into a list it's already execute it without make it parallel, ex:

  1. List&lt;List&lt;UF&gt;&gt; subSets = Lists.partition(estados, 5);
  2. List&lt;CompletableFuture&lt;List&lt;Cidade&gt;&gt;&gt; lstCompletableFuture = new ArrayList();
  3. subSets.forEach(estado -&gt; {
  4. lstCompletableFuture.add(MontarCidadesPorEstado(listaCidades, estado));
  5. });
  6. CompletableFuture.allOf(lstCompletableFuture.toArray(new CompletableFuture[lstCompletableFuture.size()]));

What I'm doing wrong, I thought it's supposed to execute the method MontarCidadesPorEstado when I call CompletableFuture.allOf.

答案1

得分: 0

我假设MontarCidadesPorEstado是一个本地方法调用。

因此,由于调用是本地的,它不会经过代理,也不是异步的。

您需要在另一个bean中定义MontarCidadesPorEstado方法,然后调用该bean。

英文:

I assume that MontarCidadesPorEstado is a local method call.

Therefore the call is local it doesn't go through a proxy and is not asynchronous.

You have define the MontarCidadesPorEstado method in another bean and then call this bean.

huangapple
  • 本文由 发表于 2020年5月29日 20:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/62086258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定