0
我創建了WorkerExecutor執行程序,並嘗試執行2個長期代碼段。我的代碼:執行後在一個線程中執行阻止代碼
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());
});
}
}
輸出:
start 1, worker-0 start 2, worker-0 finish 2, vert.x-eventloop-thread-1 finish 2, vert.x-eventloop-thread-1
爲什麼阻塞代碼只能在一個線程中執行結果?