0

I was reading ThreadPoolExecutor's source code and saw the following code.

private boolean addWorker(Runnable firstTask, boolean core) {
    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
        }
    }

Notice the retry word above. What does it do? I've never seen similar usages before. Is there a documentation about this syntax?

Searene
  • 25,920
  • 39
  • 129
  • 186

1 Answers1

0

retry is not a keyword, but an identifier. It's a label. See the Java Language Specification for the break statement.

A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; this statement, which is called the break target, then immediately completes normally. In this case, the break target need not be a switch, while, do, or for statement.

Instead of breaking out of the innermost enclosing loop as it does by default, adding the identifier causes control to transer to the part of the code identified by this label.

In this case, when execution arrives at the break retry statement, it will go to a label retry: and continue from there.