2016-12-17 68 views
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 

爲什麼阻塞代碼只能在一個線程中執行結果?

回答

1

我發現在文檔的答案,我需要執行:

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

與有序=假,否則我的代碼將連續工作,而不是在並行在同樣的背景下

相關問題