问题描述
CompletableFuture.runAsync(() -> {
// method call or code to be async.
try {
logger.info("======Before Async method call======with studySchemaEventId: "+enrollmentStudySchemaEventId);
this.performSimulation(studyId,enrollmentStudySchemaEventId,cloneOfFile,simulationRunInfo,totalAccrual);
logger.info("======After Async method call======with studySchemaEventId: "+enrollmentStudySchemaEventId);
} catch (SimulationException e) {
logger.error("Error running Async call for performSimulation()",e);
}
});
当我调用 Rest api 时,它正确执行了异步调用。 但是我有一个案例,我调用了 4 次 Rest Api,它执行了第 3 次和第 4 次 Api 调用的异步调用,我没有看到调用了异步方法。
runAsync() 调用有什么限制吗?或者为什么它在 3 次调用后不调用 Async 方法?
这是 Rest API 调用:
@POST
@Path("/trigger")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(value = "Trigger Simulation",tags = "Study Event Simulation")
public Response triggerSimulation(
@FormDataParam("file") InputStream file,@FormDataParam("file") FormDataContentdisposition fileDetail,@FormDataParam("simulationRunInfo") SimulationRunInfo simulationRunInfo
)
{
// some other logic
// Async code here
}
解决方法
您遇到的是 ForkJoinPool.commonPool()
中配置的线程数。
分配给runAsSync
的任务由ForkJoinPool.commonPool()
完成。该池是根据主机中的内核数配置的。好像你有 4 个内核。
默认情况下,公共池的大小:
Runtime.getRuntime().availableProcessors() - 1
您可以更新尺寸:
-Djava.util.concurrent.ForkJoinPool.common.parallelism=8
或者,您可以将重载的 runAsSync 与 executors 参数一起使用。