在Vertx官方文档中,我读到了以下段落。
据我所了解,如果我写错了,请纠正。Vertx的工作方式如下:
当我们为阻塞代码提供一个处理程序时,vertx从线程池中选择一个线程(不是事件循环)来运行此代码,线程池的数量等于例如我有4个核心,事件循环在每次迭代后检查此线程的状态,如果它已准备好执行与此阻塞代码相关的处理程序,则执行该处理程序,当所有4个核心都正在忙碌时,vertx将任务放到队列中以便稍后执行。
至于工作 verticle,我们有另一个线程池,默认情况下相等于20,当我们使用以下方法:
我的理解正确吗?
(PS) 根据下面的回答,使用处理程序和使用execute blocking的处理程序的阻塞代码之间没有区别。
If a result can be provided immediately, it will be returned immediately, otherwise you will usually provide a handler to receive events some time later.
Because none of the Vert.x APIs block threads that means you can use Vert.x to handle a lot of concurrency using just a small number of threads.
关于 Reactor 的文章:
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.
据我所了解,如果我写错了,请纠正。Vertx的工作方式如下:
当我们为阻塞代码提供一个处理程序时,vertx从线程池中选择一个线程(不是事件循环)来运行此代码,线程池的数量等于例如我有4个核心,事件循环在每次迭代后检查此线程的状态,如果它已准备好执行与此阻塞代码相关的处理程序,则执行该处理程序,当所有4个核心都正在忙碌时,vertx将任务放到队列中以便稍后执行。
至于工作 verticle,我们有另一个线程池,默认情况下相等于20,当我们使用以下方法:
vertx.executeBlocking()
时,我们使用该池中的线程。我的理解正确吗?
(PS) 根据下面的回答,使用处理程序和使用execute blocking的处理程序的阻塞代码之间没有区别。
//jdbc
connection.execute("insert into test values (1, 'Hello'), (2, 'World')", insert -> {
// query some data with arguments
connection.queryWithParams("select * from test where id = ?", new JsonArray().add(2), rs -> {
if (rs.failed()) {
System.err.println("Cannot retrieve the data from the database");
rs.cause().printStackTrace();
return;
}
}
//工作线程
vertx.executeBlocking(e->{},handler->{});
我的理解是否正确,这两个代码都在后台使用工作线程?