@@ -0,0 +1,2384 @@
+// Copyright 2016 The Bazel Authors. All rights reserved.%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//    http://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0Apackage com.google.devtools.build.lib.concurrent;%0A%0Aimport java.util.concurrent.ForkJoinPool;%0Aimport java.util.concurrent.ForkJoinWorkerThread;%0Aimport java.util.concurrent.atomic.AtomicLong;%0A%0A/** A %7B@link ForkJoinPool%7D with support for thread naming. */%0Apublic class NamedForkJoinPool extends ForkJoinPool %7B%0A%0A  protected NamedForkJoinPool(String name, int poolSize) %7B%0A    super(%0A        poolSize,%0A        new NamedForkJoinWorkerThreadFactory(name + %22-%25s%22),%0A        /*getUncaughtExceptionHandler=*/ null,%0A        /*asyncMode=*/ false);%0A  %7D%0A%0A  public static NamedForkJoinPool newNamedPool(String name, int numThreads) %7B%0A    return new NamedForkJoinPool(name, numThreads);%0A  %7D%0A%0A  /** A %7B@link ForkJoinWorkerThread%7D named on construction. */%0A  private static class NamedForkJoinWorkerThread extends ForkJoinWorkerThread %7B%0A%0A    public NamedForkJoinWorkerThread(ForkJoinPool forkJoinPool, String name) %7B%0A      super(forkJoinPool);%0A      this.setName(name);%0A    %7D%0A  %7D%0A%0A  /**%0A   * A factory for %7B@link NamedForkJoinWorkerThread%7Ds that names those threads using a%0A   * client-provided name format that consumes a thread index.%0A   */%0A  private static class NamedForkJoinWorkerThreadFactory implements ForkJoinWorkerThreadFactory %7B%0A%0A    private final String nameFormat;%0A    private final AtomicLong nextUnusedThreadIndex = new AtomicLong(0L);%0A%0A    public NamedForkJoinWorkerThreadFactory(String nameFormat) %7B%0A      // Fail fast if nameFormat is bad.%0A      String ignored = String.format(nameFormat, 0L);%0A      this.nameFormat = nameFormat;%0A    %7D%0A%0A    @Override%0A    public ForkJoinWorkerThread newThread(ForkJoinPool forkJoinPool) %7B%0A      return new NamedForkJoinWorkerThread(%0A          forkJoinPool, String.format(nameFormat, nextUnusedThreadIndex.getAndIncrement()));%0A    %7D%0A  %7D%0A%7D%0A

Introduce NamedForkJoinPool, a ForkJoinPool with named worker threads.