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

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

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

问题

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

@Async
private CompletableFuture<List<Cidade>> MontarCidadesPorEstado(List<Cidade> listaCidades, List<UF> estado) {
    
    estado.forEach(es -> {
            Map<String, String> param = new HashMap<String, String>();
            param.put("UF", es.getSigla());

            Cidade[] cidades = restTemplate.getForObject(url, Cidade[].class, param);
            
            listaCidades.addAll(Arrays.asList(cidades));
     });
    
    return CompletableFuture.completedFuture(listaCidades);
}

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

List<List<UF>> subSets = Lists.partition(estados, 5);
List<CompletableFuture<List<Cidade>>> lstCompletableFuture = new ArrayList();

subSets.forEach(estado -> {
     lstCompletableFuture.add(MontarCidadesPorEstado(listaCidades, estado));
});

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:

@Async
private CompletableFuture&lt;List&lt;Cidade&gt;&gt; MontarCidadesPorEstado(List&lt;Cidade&gt; listaCidades, List&lt;UF&gt; estado) {
	
	estado.forEach(es -&gt; {
		    Map&lt;String, String&gt; param = new HashMap&lt;String, String&gt;();
			param.put(&quot;UF&quot;, es.getSigla());

			Cidade[] cidades = restTemplate.getForObject(url, Cidade[].class, param);
			
			listaCidades.addAll(Arrays.asList(cidades));
	 });
	
	return CompletableFuture.completedFuture(listaCidades);
}

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:

     List&lt;List&lt;UF&gt;&gt; subSets = Lists.partition(estados, 5);
	 List&lt;CompletableFuture&lt;List&lt;Cidade&gt;&gt;&gt; lstCompletableFuture = new ArrayList();
	
	 subSets.forEach(estado -&gt; {
		 lstCompletableFuture.add(MontarCidadesPorEstado(listaCidades, estado));
	 });

	 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:

确定