为什么 Vert.x 每个线程附加 2 个事件循环

Why Vert.x attaches 2 event loop per thread

在阅读 vert.x Java 开发指南时,我发现 Vert.x 每个 CPU 线程附加了 2 个事件循环。与每个 CPU 线程 1 个事件循环相比,这对性能有何影响?

这个概念叫做Multi-Reactor。 Event Loop 是 Reactor 设计模式的一种实现。 事件循环需要检查新事件并将它们分派给相应的处理程序。如果我们只使用 1 个线程来处理所有事件,我们就不会最大限度地利用我们的硬件。在其他语言和框架中,多个 thread/processes 用于此目的。示例:Node.js 这就是为什么我们有 Multi-Reactor.

In a standard reactor implementation there is a single event loop thread which runs around in a loop delivering all events to all handlers as they arrive. The trouble with a single thread is it can only run on a single core at any one time, so if you want your single threaded reactor application (e.g. your Node.js application) to scale over your multi-core server you have to start up and manage many different processes. Vert.x works differently here. Instead of a single event loop, each Vertx instance maintains several event loops. By default we choose the number based on the number of available cores on the machine, but this can be overridden. This means a single Vertx process can scale across your server, unlike Node.js. We call this pattern the Multi-Reactor Pattern to distinguish it from the single threaded reactor pattern.

Full explanation

在文档中它说 Vert.x 每个 core CPU 线程创建两个事件循环,即每个超线程 1 个事件循环。