Home | History | Annotate | Download | only in concurrent
      1 /*
      2  * Copyright (C) 2006 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.util.concurrent;
     18 
     19 import java.util.concurrent.Executors;
     20 import java.util.concurrent.ThreadFactory;
     21 import java.util.concurrent.atomic.AtomicInteger;
     22 
     23 /**
     24  * A ThreadFactory which decorates another ThreadFactory to set a name on
     25  * each thread created.
     26  *
     27  * @author Kevin Bourrillion
     28  * @since 2009.09.15 <b>tentative</b>
     29  */
     30 public class NamingThreadFactory implements ThreadFactory {
     31   private final ThreadFactory backingFactory;
     32   private final String format;
     33   private final AtomicInteger count = new AtomicInteger(0);
     34 
     35   public static final ThreadFactory DEFAULT_FACTORY
     36       = Executors.defaultThreadFactory();
     37 
     38   /**
     39    * Creates a new factory that delegates to the default thread factory for
     40    * thread creation, then uses {@code format} to construct a name for the new
     41    * thread.
     42    *
     43    * @param format a {@link String#format(String, Object...)}-compatible format
     44    *     String, to which a unique integer (0, 1, etc.) will be supplied as the
     45    *     single parameter. This integer will be unique to this instance of
     46    *     NamingThreadFactory and will be assigned sequentially.
     47    */
     48   public NamingThreadFactory(String format) {
     49     this(format, DEFAULT_FACTORY);
     50   }
     51 
     52   /**
     53    * Creates a new factory that delegates to {@code backingFactory} for thread
     54    * creation, then uses {@code format} to construct a name for the new thread.
     55    *
     56    * @param format a {@link String#format(String, Object...)}-compatible format
     57    *     String, to which a unique integer (0, 1, etc.) will be supplied as the
     58    *     single parameter
     59    * @param backingFactory the factory that will actually create the threads
     60    * @throws java.util.IllegalFormatException if {@code format} is invalid
     61    */
     62   public NamingThreadFactory(String format, ThreadFactory backingFactory) {
     63     this.format = format;
     64     this.backingFactory = backingFactory;
     65     makeName(0); // fail fast if format is bad
     66   }
     67 
     68   public Thread newThread(Runnable r) {
     69     Thread t = backingFactory.newThread(r);
     70     t.setName(makeName(count.getAndIncrement()));
     71     return t;
     72   }
     73 
     74   private String makeName(int ordinal) {
     75     return String.format(format, ordinal);
     76   }
     77 }
     78