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

huangapple go评论77阅读模式
英文:

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

问题

所以我面临着这样的情况:我正在编写一个 Java 应用程序来对某些事件做出反应。接收到的每个事件都需要进行处理,处理可能需要一些时间。因此,我的应用程序基本上在执行以下操作:

  • 它接收事件。
  • 它调用 CompletableFuture.runAsync(() -> EventProcessor.process(event));

即:

class EventHandler implements Handler {

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

}

我的问题是:

  • 如果我接收到大量事件会发生什么?由于处理一个事件可能需要一些时间(例如 10 分钟)。
  • 后台任务是如何管理和调度的?它们会被终止吗?如果会,我如何控制调度/终止行为?
英文:

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

i.e

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 (e.g. 10 minutes)
  • How are the backgrounds tasks managed and scheduled? would they get killed? if yes, how can I control the scheduling/termination behavior?

答案1

得分: 2

以下是翻译好的部分:

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

新任务占用一些内存,因此可能会出现内存溢出异常。然后,它们会被加入到执行器的队列中。在加入队列时,如果执行器的输入队列大小受限,可能会抛出RejectedExecutionException异常。

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

如果任务成功提交,迟早会被执行。只有您可以明确取消它。当然,如果您提交了1000个需要10分钟的任务,您可能需要等待几天才能得到结果。

英文:

What happens if I receive plenty of events? Since processing an event can take some time (e.g. 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.

huangapple
  • 本文由 发表于 2020年9月14日 22:32:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63886402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定