Blocking code is executed in the one thread

757 Views Asked by At

I created WorkerExecutor executor and tried execute 2 long-term piece of code. My code:

public class BVerticle extends AbstractVerticle {
    @Override
    public void start() throws Exception {
        WorkerExecutor executor = vertx.createSharedWorkerExecutor("worker", 10, 10000);
        executor.executeBlocking(future -> {
            out.printf("start 1, %s \n", Thread.currentThread().getName());
            try {
                Thread.sleep(5_000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            future.complete();
        }, result -> {
            out.printf("finish 2, %s \n", Thread.currentThread().getName());
        });
        executor.executeBlocking(future -> {
            out.printf("start 2, %s \n", Thread.currentThread().getName());
            try {
                Thread.sleep(5_000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            future.complete();
        }, result -> {
            out.printf("finish 2, %s \n", Thread.currentThread().getName());
        });
    }
}

Output after execution:

start 1, worker-0
start 2, worker-0
finish 2, vert.x-eventloop-thread-1
finish 2, vert.x-eventloop-thread-1

Why blocking code is executed only in the one thread consequently?

1

There are 1 best solutions below

0
On BEST ANSWER

I found answer in documentation, I need to execute:

executeBlocking(Handler<Future<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<T>> resultHandler)

with ordered = false, otherwise my code would work serially, not in parallel in the same context