问题描述
我对 Vertx 中的标准 verticle 和 worker verticle 感到困惑.它们的用例是什么?
I am confused about standard verticle and worker verticle in Vertx.And What are the use cases of them?
推荐答案
Vert.x 是一个事件驱动的非阻塞工具包.当一个标准"verticle 被执行时,它的逻辑在一个事件循环线程上运行.虽然这个线程运行那个 Verticle 的逻辑,但它不能服务任何其他请求,所以这个线程不应该执行任何阻塞代码.
Vert.x is an event-driven and non-blocking toolkit. When a "standard" verticle gets executed, its logic runs on an event loop thread. Whilst this thread runs the logic of that verticle, it cannot serve any other request, so this thread should not execute any blocking code.
但是,有时,您确实需要执行阻塞代码 — 进行长时间计算、同步调用外部服务等 — 在这种情况下,您需要确保不会发生这种情况在事件循环线程上.
But, sometimes, you do need to execute blocking code — doing a long computation, calling an external service synchronously, etc. — in which case, you need to make sure this doesn't happen on an event loop thread.
执行阻塞代码有两种方式:
You have two ways to execute blocking code:
- 使用worker verticle,它将在其自己的线程池,可以根据需要进行配置;
- 或者使用
executeBlocking
块,它在单独的线程中执行.
- Use a worker verticle, which will execute code in its own thread pool, that can you can configure as needed;
- Or use an
executeBlocking
block, which executes in a separate thread.
这篇关于标准verticle和workerverticle的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!