我在我的 freebsd 1CPU/2GB 服务器上使用 Spray.io 和 akka.io,我面临着 线程问题。当我遇到 OutOfMemory 异常时我开始注意到它 因为“无法创建 native 线程”。
我通常检查Thread.activeCount()
,我发现它增长得非常快。
目前我使用这些设置:
myapp-namespace-akka {
akka {
loggers = ["akka.event.Logging$DefaultLogger"]
loglevel = "DEBUG"
stdout-loglevel = "DEBUG"
actor {
deployment {
default {
dispatcher = "nio-dispatcher"
router = "round-robin"
nr-of-instances = 1
}
}
debug {
receive = on
autoreceive = on
lifecycle = on
}
nio-dispatcher {
type = "Dispatcher"
executor = "fork-join-executor"
fork-join-executor {
parallelism-min = 8
parallelism-factor = 1.0
parallelism-max = 16
task-peeking-mode = "FIFO"
}
shutdown-timeout = 4s
throughput = 4
throughput-deadline-time = 0ms
attempt-teamwork = off
mailbox-requirement = ""
}
aside-dispatcher {
type = "Dispatcher"
executor = "fork-join-executor"
fork-join-executor {
parallelism-min = 8
parallelism-factor = 1.0
parallelism-max = 32
task-peeking-mode = "FIFO"
}
shutdown-timeout = 4s
throughput = 4
throughput-deadline-time = 0ms
attempt-teamwork = on
mailbox-requirement = ""
}
}
}
}
我希望nio-dispatcher
成为我的默认非阻塞(假设是单线程)
调度员。我在 aside-dispatcher
上执行所有的 future(数据库、网络查询)。
我通过我的应用程序获取上下文,如下所示:
trait Contexts {
def system: ActorSystem
def nio: ExecutionContext
def aside: ExecutionContext
}
object Contexts {
val Scope = "myapp-namespace-akka"
}
class ContextsImpl(settings: Config) extends Contexts {
val System = "myapp-namespace-akka"
val NioDispatcher = "akka.actor.nio-dispatcher"
val AsideDispatcher = "akka.actor.aside-dispatcher"
val Settings = settings.getConfig(Contexts.Scope)
override val system: ActorSystem = ActorSystem(System, Settings)
override val nio: ExecutionContext = system.dispatchers.lookup(NioDispatcher)
override val aside: ExecutionContext = system.dispatchers.lookup(AsideDispatcher)
}
// Spray trait mixed to service actors
trait ImplicitAsideContext {
this: EnvActor =>
implicit val aside = env.contexts.aside
}
我认为我确实搞乱了配置或实现。帮帮我吧。 通常我现在在我的应用程序上看到数千个线程,直到它崩溃(我设置 freebsd 预处理限制为 5000)。
最佳答案
如果您的应用程序确实启动了如此多的线程,这通常可以追溯到 ForkJoinPools 内的阻塞行为(糟糕的坏事!),我在此处的答案中详细解释了该问题:blocking blocks ,因此您可能需要在那里阅读并验证您的应用程序中正在创建哪些线程以及原因 - ForkJoinPool 没有静态上限。
关于java - 使用 Spray Akka 超线程的 Scala 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826387/