专栏名称: go4it
目录
相关文章推荐
阳光海南网  ·  报名进行中!2025海南省社区运动会乒乓球比 ... ·  22 小时前  
阳光海南网  ·  报名进行中!2025海南省社区运动会乒乓球比 ... ·  22 小时前  
深圳晚报  ·  价格“疯”涨!最高6880元 ·  2 天前  
深圳晚报  ·  价格“疯”涨!最高6880元 ·  2 天前  
封面新闻  ·  刚刚,中国乒协发声! ·  2 天前  
封面新闻  ·  刚刚,中国乒协发声! ·  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;
    }
}
复制代码






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