简体   繁体   English

如果对 CompletableFuture.runAsync() 调用过多会发生什么?

[英]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.所以我面临的情况是我正在编写一个 Java 应用程序来对某些事件做出反应。 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));它调用CompletableFuture.runAsync(() -> EventProcessor.process(event));

ie 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)由于处理事件可能需要一些时间(例如 10 分钟)
  • 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)由于处理事件可能需要一些时间(例如 10 分钟)

new tasks occupy some memory, so OutOfMemoryException can occur.新任务会占用一些内存,因此可能会发生 OutOfMemoryException。 Then, they are enqueued to an Executor.然后,他们被排队到一个 Executor。 When enqueueing, RejectedExecutionException can be thrown if the input queue of the executor is limited in size.入队时,如果executor的输入队列有大小限制,可以抛出RejectedExecutionException。

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.当然,如果你提交1000个10分钟的任务,你可以等待几天的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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