JAVA-retry 重试

在看 ThreadPoolExecutor 源码时看到这么一段代码

retry:
for (;;) {
    int c = ctl.get();
    int rs = runStateOf(c);

    // Check if queue empty only if necessary.
    if (rs >= SHUTDOWN &&
            ! (rs == SHUTDOWN &&
                    firstTask == null &&
                    ! workQueue.isEmpty()))
        return false;

    for (;;) {
        int wc = workerCountOf(c);
        if (wc >= CAPACITY ||
                wc >= (core ? corePoolSize : maximumPoolSize))
            return false;
        if (compareAndIncrementWorkerCount(c))
            break retry;
        c = ctl.get();  // Re-read ctl
        if (runStateOf(c) != rs)
            continue retry;
        // else CAS failed due to workerCount change; retry inner loop
    }
}

 

break 和 continue 分开测试

retry:
for (;;) {
    System.out.println("A");
    for (;;) {
        System.out.println("B");
        break retry;
    }
}
System.out.println("End");

retry:
for (; ; ) {
    System.out.println("A");
    for (; ; ) {
        System.out.println("B");
        continue retry;
    }
}

会无限循环

 

总结

retry 并不是一个关键字,只是作为一个标记使用。并与最近的一个循环绑定,在使用 break 或 continue 时后面可加上该标记,就可指定对哪一层循环进行操作了

转载于:https://www.cnblogs.com/jhxxb/p/10830002.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值