How vert.x is single threaded?
Asked Answered
H

2

9

From my understanding, each vert.x instance will be assigned an event loop. Event loop handles all the request an other task for that particular instance. Event loop is a thread, I think. When there are multiple vert.x instance deployed, each instance have there own event loops right? That means there are multiple thread(multi-threading) exists. This is how I understood. This single-threading concept causing me so much headache. Any help will be appreciated.

Homocercal answered 29/5, 2014 at 10:28 Comment(2)
More than one event loop means no multi-threading?Homocercal
When you say "vert.x instance" do you mean "verticle instance"?Politician
K
14

Vert.x is not single threaded like node.js. In vert.x you have to differentiate between Verticles and WorkerVerticles. One Vert.x instance will create several event loops (which are threads), usually one per CPU-core. The Verticles will be assigned to an event loop and will always be executed by the same thread, but only if there is work to do. The WorkerVerticles will be assigned to a thread coming from the worker pool and can be executed by different threads. What makes programming fun with Vert.x is the fact that you can "pretend" it is single threaded. You do not have to care about concurrency because you cannot share variables between verticles. Each verticle runs in its own classloader and you can only share data via the event bus. (There is also a shared map but that is distracting in this case.)

Kostroma answered 11/11, 2014 at 21:47 Comment(3)
"The Verticles will be assigned to an event loop and will always be executed by the same thread, but only if there is work to do". This sounds really bad. That means you could have two busy verticals on the same thread blocking each other, while other threads lie idle. Golang does it correctly - goroutines that are ready are always assigned to the next available thread.Depose
For a simple REST server, my guess is you don't want do db calls with a worker verticle and then send the data via the eventBus, because that's too expensive. I assume using io.vertx.ext.web.Router to handle requests in the main verticle is best. But I don't know if the MainVerticle code is always running the same thread - my guess is that it is.Extortioner
@Depose please see Don’t call us, we’ll call you.Carissacarita
C
9

You probably figured it out by now, but I am posting an answer for other people that might reach this question.

A vert.x instance will create multiple threads. Inside that vert.x instance you have multiple verticles (verticle instances more accurately) running. It is a verticle instance that has an event loop assigned, but that event loop is not exclusively assigned to that verticle.

Overlaps can happen when you are running more verticles that the number of threads. In that case some of the verticles will be assigned to the same event loop as other verticles. That event loop will process events from all the assigned verticles.

But, once a verticle instance is started and assigned to an event loop/thread, it will run on it until the verticle is terminated.

Channelize answered 12/9, 2014 at 23:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.