Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How vert.x is single threaded?

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.

like image 388
Ronald Randon Avatar asked May 29 '14 10:05

Ronald Randon


2 Answers

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.)

like image 161
Mr__Steel Avatar answered Oct 06 '22 03:10

Mr__Steel


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.

like image 36
Andrei Stanescu Avatar answered Oct 06 '22 04:10

Andrei Stanescu