私は答えを2つに分けます。 RxJavaに依存するのではなく、通常のJavaだけに依存します。
まず、より複雑な例に今A_OPERATION
Vertx vertx = Vertx.vertx();
CountDownLatch latch = new CountDownLatch(1);
Long start = System.currentTimeMillis();
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start() throws InterruptedException {
// Just to demonstrate
Thread.sleep(1000);
latch.countDown();
}
});
// Always use await with timeout
latch.await(2, TimeUnit.SECONDS);
System.out.println("Took me " + (System.currentTimeMillis() - start) + " millis");
待つ:
public static void main(String[] args) throws InterruptedException {
Vertx vertx = Vertx.vertx();
// This should be equal to number of operations to complete
CountDownLatch latch = new CountDownLatch(3);
Long start = System.currentTimeMillis();
// Start your operations
vertx.deployVerticle(new BlockingVerticle(latch));
vertx.deployVerticle(new BlockingVerticle(latch));
vertx.deployVerticle(new BlockingVerticle(latch));
// Always use await with timeout
latch.await(2, TimeUnit.SECONDS);
System.out.println("Took me " + (System.currentTimeMillis() - start) + " millis");
}
private static class BlockingVerticle extends AbstractVerticle {
private final CountDownLatch latch;
public BlockingVerticle(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void start() throws InterruptedException {
long millis = 1000 + ThreadLocalRandom.current().nextInt(500);
System.out.println("It will take me " + millis + " to complete");
// Wait for some random time, but no longer that 1.5 seconds
Thread.sleep(millis);
latch.countDown();
}
}
は、あなたがそのメインスレッドが最大(B_OPERATION、C_OPERATION、D_OPERATION)+少数のためにブロックされます注意してくださいミリ秒以上。
が答えてくれてありがとう!しかし、それは同期のために余分な頂点を配置するのは私にとっては奇妙に思えます。私のアプリは '-ha'オプションで動作しているので、それらをデプロイしたくありません。つまり、各頂点のオーバヘッドはほとんどありません。 (私も嫌いな)解決策として 'vertx.executeBlocking'の中に' CountDownLatch'を渡すことができます。しかしvertxのdocsは、vertexはあなたのコードを同期コードから解放することになっており、それは間違った方法であると言います。 –
まず、あなたは絶対に正しいです。私はあなたがブロックコードを望んでいたという特別な理由があったと思いました。先物に基づいて同じコードで別の回答を追加します。 –