专栏名称: go4it
目录
相关文章推荐
CDA数据分析师  ·  CDA数据人才能力模型与认证体系简介​ ·  昨天  
中国交建  ·  天山上的“南水北调” ·  11 小时前  
中国城市规划  ·  两会声音 | ... ·  昨天  
中国城市规划  ·  天下事 | “千万人口”城市+1 ·  2 天前  
51好读  ›  专栏  ›  go4it

聊聊cheddar的PendingResult

go4it  · 掘金  ·  · 2021-03-28 23:42

正文

阅读 1

聊聊cheddar的PendingResult

本文主要研究一下cheddar的PendingResult

PendingResult

Cheddar/cheddar/cheddar-application/src/main/java/com/clicktravel/cheddar/application/pending/result/PendingResult.java

class PendingResult {

    private static final long TIMEOUT_SECONDS = 30;
    private final CountDownLatch countdownLatch = new CountDownLatch(1);
    private Result result;

    /**
     * Offers a {@link Result}, returning immediately.
     * @param result {@link Result} to offer
     */
    public void offerResult(final Result result) {
        this.result = result;
        countdownLatch.countDown();
    }

    /**
     * Polls for a {@link Result}, blocking until it is offered by some other thread. If the result has already been
     * offered, the result is returned immediately.
     * @return {@link Result} obtained, potentially after blocking
     * @throws InterruptedException, PendingResultTimeoutException
     */
    public Result pollResult() throws InterruptedException, PendingResultTimeoutException {
        if (!countdownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
            throw new PendingResultTimeoutException();
        }
        return result;
    }
}
复制代码






请到「今天看啥」查看全文