Java 使用 CountDownLatch 轮询方法直到成功响应

Java Using CountDownLatch to poll a method until a success response

我试图每 60 秒多次调用一个方法,直到该方法成功响应,该方法实际上调用了不同服务上的休息端点。截至目前,我正在使用 do while 循环并使用

Thread.sleep(60000);

让主线程等待 60 秒,由于并发问题,我觉得这不是理想的方式。

我遇到了 CountDownLatch 方法使用

CountDownLatch latch = new CountDownLatch(1);
boolean processingCompleteWithin60Second = latch.await(60, TimeUnit.SECONDS);

@Override
public void run(){

    String processStat = null;
    try {
        status = getStat(processStatId);
        if("SUCCEEDED".equals(processStat))
        {
            latch.countDown();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }   
}

我在另一个 class 中有 运行 方法,它实现了 运行nable。无法正常工作。知道哪里出了问题吗?

您可以使用 CompletableFuture 而不是 CountDownLatch 来 return 结果:

CompletableFuture<String> future = new CompletableFuture<>();

invokeYourLogicInAnotherThread(future);

String result = future.get(); // this blocks

并且在另一个线程中(可能在循环中):

@Override
public void run() {

    String processStat = null;
    try {
        status = getStat(processStatId);
        if("SUCCEEDED".equals(processStat))
        {
            future.complete(processStat);
        }
    } catch (Exception e) {
        future.completeExceptionally(e);
    }   
}

future.get() 将阻塞,直到通过 complete() 方法和 return 提交的值提交某些内容,否则它将抛出通过 completeExceptionally() 提供的包含在 ExecutionException.

还有get()版本有超时限制:

String result = future.get(60, TimeUnit.SECONDS);

终于使用 Executor Framework 让它工作了。

            final int[] value = new int[1];
            pollExecutor.scheduleWithFixedDelay(new Runnable() {

                Map<String, String> statMap = null;

                @Override
                public void run() {

                    try {
                        statMap = coldService.doPoll(id);
                    } catch (Exception e) {

                    }
                    if (statMap != null) {
                        for (Map.Entry<String, String> entry : statMap
                                .entrySet()) {
                            if ("failed".equals(entry.getValue())) {
                                value[0] = 2;

                                pollExecutor.shutdown();
                            }
                        }
                    }
                }

            }, 0, 5, TimeUnit.MINUTES);
            try {
                pollExecutor.awaitTermination(40, TimeUnit.MINUTES);
            } catch (InterruptedException e) {

            }