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

  • 相关阅读:
    ELF静态链接
    linux
    百度图表插件
    秀米(图文编辑)
    Html 助手
    在型原型工具
    AnalyticDB
    H5 (webApi) 接口帮助文档
    H5 拖拽读取文件和文件夹
    mongodb int字段的一个小坑
  • 原文地址:https://www.cnblogs.com/jhxxb/p/10830002.html
Copyright © 2011-2022 走看看