java - 为什么 Vert.x 被称为响应式,尽管它是单线程的

标签 java multithreading asynchronous vert.x

我从 Vert.x 文档(以及其中的一些代码)中了解到,Vert.x 是单线程,并在事件池中执行事件。它不等待 I/O 或任何网络操作,而是为另一个事件提供时间(这在任何 Java 多线程框架中都没有)。

但我无法理解以下内容:

  1. 单线程比多线程好在哪里?如果有数百万个传入 HTTP 请求怎么办?会不会比其他多线程框架慢?
  2. Verticle 依赖于 CPU 核心。您拥有多少个 CPU 核心,就可以有多少个 verticle 并行运行。为什么运行在虚拟机上的语言可以按需使用CPU呢?据我所知,Java VM (JVM) 是一个仅使用另一个操作系统进程的应用程序(这里我对操作系统和 JVM 的理解较少,因此我的问题可能很天真)。
  3. 如果单线程、非阻塞概念如此有效,那么为什么我们不能在多线程环境中拥有相同的非阻塞概念呢?不是会更快吗?或者说,是因为 CPU 一次只能执行一个线程吗?

最佳答案

What I understood from Vert.x documentation (and a little bit of coding in it) is that Vert.x is single threaded and executes events in the event pool.

它是事件驱动的、基于回调的。 It isn't single-threaded :

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.

It doesn't wait for I/O or any network operation(s)

它使用非阻塞或异步 I/O,目前尚不清楚是哪一种。使用 Reactor 模式表明是非阻塞的,但也可能不是。

rather than giving time to another event (which was not before in any Java multi-threaded framework).

这没有意义。

  1. How single thread is better than multi-threaded?

事实并非如此。

What if there are millions of incoming HTTP requests? Won't it be slower than other multi-threaded frameworks?

是的。

  1. Verticles depend on CPU cores. As many CPU cores you have, you can have that many verticles running in parallel. How come a language that works on a virtual machine can make use of CPU as needed? As far as I know, the Java VM (JVM) is an application that uses just another OS process for (here my understanding is less about OS and JVM hence my question might be naive).

它使用每个核心一个线程,根据上面的引用,或者您通过覆盖它选择的任何内容。

  1. If a single threaded, non-blocking concept is so effective then why can't we have the same non-blocking concept in a multi-threaded environemnt?

你可以。

Won't it be faster?

是的。

Or again, is it because CPU can execute one thread at a time?

多核 CPU 一次可以执行多个线程。我不知道“是因为”中的“它”指的是什么。

关于java - 为什么 Vert.x 被称为响应式,尽管它是单线程的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45610699/

相关文章:

java - java.util.concurrent.BrokenBarrierException 的原因

java - 如何使用 JAX-RS 流式传输无休止的 InputStream

java - Spring Boot 2 和 JDK 10 的 Spring Tool Suite 构建错误

java - 为什么 Files.readAllBytes 首先读取 bufsize 为 1?

asp.net - 线程如何通过 Http.sys、IIS 和 ASP.NET 与请求绑定(bind)

c++ - 为什么我的并行 foreach 循环实现比单线程慢?

c++ - 从 WinRT 中的异步任务 lambda 更新 UI

c++ - Boost::thread 如何在主线程和工作线程之间同步?

javascript - 在 dom 准备好后,从 <img> 获取所有数据源并将它们设置在 src 属性中

c# - 每 3 秒显示一次程序运行时间