简体   繁体   中英

Is there any thread limit on CompletableFuture.runAsync

I have a Rest api, in which it calls a async call like below

 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);
                        }
                    });

when i call Rest api, it executed async call correctly. But i have a case where i called Rest Api, 4 times and it executed Async call for 3 and for the 4th Api call i dont see Async method being called.

Is there any limit on runAsync() call? or why is it not calling Async method after 3 calls?

Here is the Rest API call:

    @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

}

What you have encountered is the number of threads configured in the ForkJoinPool.commonPool() .

The task assigned to runAsSync is completed by the ForkJoinPool.commonPool() . This pool is configured on the basis of the number of cores in your host computer. It seems that you have 4 cores.

By default, the size of the common pool:

Runtime.getRuntime().availableProcessors() - 1

You can update the size:

-Djava.util.concurrent.ForkJoinPool.common.parallelism=8

Alternatively you can use the overloaded runAsSync with the executors argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM