简体   繁体   中英

What happens if you make too many calls to CompletableFuture.runAsync()?

so I'm facing a situation where I'm writing a java application to react to some events. Each event received needs to be handled and the handling could take some time. So what my application is doing the following basically:

  • It receives the event.
  • It calls CompletableFuture.runAsync(() -> EventProcessor.process(event));

ie

Class EventHandler implements Handler {

   public void handleEvent(Event event) {
      CompletableFuture.runAsync(() -> EventProcessor.process(event));
   }

}

My questions are:

  • What happens if I receive plenty of events? Since processing an event can take some time (eg 10 minutes)
  • How are the backgrounds tasks managed and scheduled? would they get killed? if yes, how can I control the scheduling/termination behavior?

What happens if I receive plenty of events? Since processing an event can take some time (eg 10 minutes)

new tasks occupy some memory, so OutOfMemoryException can occur. Then, they are enqueued to an Executor. When enqueueing, RejectedExecutionException can be thrown if the input queue of the executor is limited in size.

How are the backgrounds tasks managed and scheduled? would they get killed? if yes, how can I control the scheduling/termination behavior?

if task is successfully submitted, it will be executed sooner of later. Only you can cancel it explicitly. Of course, if you submit 1000 10-minute tasks, you can wait the result for several days.

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