java如何在单独的线程中执行大量WS调用并增加单个结果变量?
线程初学者在这里
在Struts操作中,我需要通过多次调用Web服务来检索一些数据,每次调用不同的请求,并等待每个请求完成,以便显示结果
所以基本上我是这样做的:
// The list of the region codes used for the requests
List<String> codesRegions;
// Init of a variable containing the total amount of data
Integer totalAmount = 0;
// For every region
for(String codeRegion : codesRegions)
{
MyRegionStub stub = createRegionStub();
// Call of the WS with the code region
MyRegionRequest request = new MyRegionRequest();
request.setCodeRegion(codeRegion);
// Getting the number associated to the region and incrementing totalAmount
MyRegionResponse response = stub.getRegionStats(request);
totalAmount += response.getRegionStats();
}
// Once all the calls are done, I display the result
request.setAttribute("totalAmount", totalAmount);
mapping.findForward("success");
我的问题是,由于我多次调用此WS,因此经常会出现超时错误。所以我想知道如何在一个单独的线程中执行每个调用,并毫无错误地递增相同的结果变量
我想知道的另一件事是,我需要等待所有调用完成后才能显示数据。我需要做什么才能在完成后调用mapping.findForward("success");
# 1 楼答案
您可以将
Executor framework
与CountDownLatch
一起使用。ExecutorService
将在线程池中执行异步任务,CountDownLatch
将等待所有任务完成在这种情况下,您应该注意
countDownLatch.countDown();
必须出现在finally
块中,这样它就可以保证被执行,并且对于总数量,您应该使用线程安全的AtomicInteger
您的代码如下所示:
# 2 楼答案
如果您使用的是java 8,我建议CompletableFuture
这将允许您创建一个运行异步的线程负载,比如
一旦你创建了所有的未来,你就可以检查它们何时全部完成
然后,对于你的每一个未来,你可以做价值总和