Why does Vert.x create a new event loop for an http server?

Viewed 1251

I have a very simple Vert.x application that exposes a ping endpoint:

LauncherVerticle.java

public class LauncherVerticle extends AbstractVerticle {
    @Override
    public void start(Future<Void> future) throws Exception {
        DeploymentOptions options = new DeploymentOptions();
        options.setConfig(config());
        options.setInstances(1);

        String verticleName = Example1HttpServerVerticle.class.getName();
        vertx.deployVerticle(verticleName, options, ar -> {
            if (ar.succeeded()) {
                future.complete();
            } else {
                future.fail(ar.cause());
            }
        });
    }
}

PingVerticle.java

public class PingVerticle extends AbstractVerticle {
    @Override
    public void start(Future<Void> future) throws Exception {
        Router router = Router.router(vertx);

        router.get("/ping").handler(context -> {
            String payload = new JsonObject().put("hey", "ho").encode();
            context.response().putHeader("content-type", "application/json").end(payload);
        });
    }
}

As expected, by default Vert.x creates two event loop threads that I can see with VisualVM:

2 event loop threads

Of course, the application doesn't do anything, so I know go and add an http server to PingVerticle:

    String host = "0.0.0.0";
    int port = 7777;
    vertx.createHttpServer().requestHandler(router::accept).listen(port, host, ar -> {
        if (ar.succeeded()) {
            future.complete();
        } else {
            future.fail(ar.cause());

        }
    });

Now I see in VisualVM that there are two new threads, an acceptor-thread, that I can more or less understand, and another eventloop-thread:

3 eventloop threads

Why is this third eventloop-thread created?

2 Answers

According to vert.x javadoc:

The default number of event loop threads to be used = 2 * number of cores on the machine.

It seems, you have more than 1 core.

Related