gpt4 book ai didi

java - 在Java中设置Actor系统的默认ExecutionContext

转载 作者:行者123 更新时间:2023-12-02 10:57:49 24 4
gpt4 key购买 nike

我想在创建 Actor System 时将 Java 中的 ExecutorService 对象的引用作为默认调度程序/executionContext 传递。有没有办法做到这一点,而不是从配置中读取调度程序属性。

最佳答案

使用ActorSystem工厂方法create它采用 ExecutionContext 作为参数。以下是方法签名和 Javadoc(重点是我的):

public static ActorSystem create(java.lang.String name,
com.typesafe.config.Config config,
java.lang.ClassLoader classLoader,
scala.concurrent.ExecutionContext defaultExecutionContext)

Creates a new ActorSystem with the specified name, the specified Config, the specified ClassLoader, and the specified ExecutionContext. The ExecutionContext will be used as the default executor inside this ActorSystem. If null is passed in for the Config, ClassLoader and/or ExecutionContext parameters, the respective default value will be used. If no Config is given, the default reference config will be obtained from the ClassLoader. If no ClassLoader is given, it obtains the current ClassLoader by first inspecting the current threads' getContextClassLoader, then tries to walk the stack to find the callers class loader, then falls back to the ClassLoader associated with the ActorSystem class. If no ExecutionContext is given, the system will fallback to the executor configured under "akka.actor.default-dispatcher.default-executor.fallback". Note that the given ExecutionContext will be used by all dispatchers that have been configured with executor = "default-executor", including those that have not defined the executor setting and thereby fallback to the default of "default-dispatcher.executor".

例如:

import scala.concurrent.ExecutionContext;

final ExecutionContext ec = ...

ActorSystem system = ActorSystem.create("MySystem", null, null, ec);

关于java - 在Java中设置Actor系统的默认ExecutionContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51583993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com