[concurrency-interest] Setting the name of a Thread

Ernst, Matthias matthias.ernst at coremedia.com
Wed Aug 2 11:24:40 EDT 2006


> I have a ThreadPoolExecutor that runs an arbitrary number of 
> housekeeping tasks in a system.  Each of these tasks 
> implements an interface extends Runnable and has getName().  
> I want to submit each of these to the executor and have the 
> name of the thread set to the getName().

Why don't you submit wrappers that implement the name-setting?

execute(final NamedRunnable namedRunnable) {
  tpe.execute(new Runnable() {
    public void run() {
      Thread t = Thread.currentThread();
      String oldName = t.getName();
      t.setName(namedRunnable.getName());
      try {
        namedRunnable.run();
      } finally {
        t.setName(oldName);
      }
    }
  });
}

Matthias



More information about the Concurrency-interest mailing list