All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleframework.util.thread.ThreadNamer Maven / Gradle / Ivy

Go to download

Simple is a high performance asynchronous HTTP server for Java

There is a newer version: 5.1.6
Show newest version
/*
 * ThreadNamer.java February 2009
 *
 * Copyright (C) 2009, Niall Gallagher 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General 
 * Public License along with this library; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA  02111-1307  USA
 */

package org.simpleframework.util.thread;

import java.util.concurrent.atomic.AtomicInteger;

import org.simpleframework.util.KeyMap;

/**
 * The ThreadNamer object is used to name threads using
 * sequence numbers. Each thread with the same name will be given a
 * unique sequence number which is appended to the end of the name.
 * This is similar to the Java thread naming convention used by the
 * standard thread and thread pool implementations.
 * 
 * @author Niall Gallagher
 */
class ThreadNamer {
   
   /**
    * This is the singleton sequencer that is used to track names.
    */
   private static final Sequencer SEQUENCER;
   
   static {
      SEQUENCER = new Sequencer();
   }
   
   /**
    * This will create a thread name that is unique. The thread name
    * is a combination of the provided name and a sequence number
    * which is appended to the end of the name. This ensures that
    * each thread within the system has a unique name.
    * 
    * @param name this is the prefix for the thread name produced
    * 
    * @return this will return the name of the thread produced
    */
   public static String getName(String name) {
      int count = SEQUENCER.next(name);

      if(name == null) {
         return null;
      }
      return String.format("%s-%s", name, count);
   }
   
   /**
    * The Sequencer is used to create sequence numbers
    * for the threads that are to be named. This basically uses a
    * hash map of strings to atomic integers. When a name is used
    * the integer is incremented and returned.
    * 
    * @author Niall Gallagher
    */
   private static class Sequencer {
      
      /**
       * This is the map of atomic integers that are referenced.
       */
      private final KeyMap map;
      
      /**
       * Constructor for the Sequencer object. This is
       * used to keep track of the sequence numbers used for the
       * threads in the system, so that they are all unique.
       */
      public Sequencer() {
         this.map = new KeyMap();
      }
      
      /**
       * This is used to get the next sequence number for the name
       * provided. This allows the thread namer to construct a 
       * unique sequence number for threads with the same name.
       * 
       * @param name this is the name of the thread to sequence
       * 
       * @return this is the sequence number that has been retrieved
       */
      public synchronized int next(String name) {
         AtomicInteger count = map.get(name);
         
         if(count == null) {
            count = new AtomicInteger();
            map.put(name, count);
         }
         return count.getAndIncrement();
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy