zoukankan      html  css  js  c++  java
  • 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 时后面可加上该标记,就可指定对哪一层循环进行操作了

  • 相关阅读:
    winform中key读取修改
    验证时间的正则表达式
    oracle 死锁
    SQL中GETDATE()一些操作
    SQL查询优化
    .config 中特殊字符的处理
    判断二个时间是否在同一周内
    Repeater嵌套(灵活的)
    获取同一字段不同的值
    泛型详解
  • 原文地址:https://www.cnblogs.com/jhxxb/p/10830002.html
Copyright © 2011-2022 走看看