如何解决CompletableFuture.runAsync 上是否有任何线程限制
我有一个 Rest api,它调用如下异步调用
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 参数一起使用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。