Setting the name of a Thread

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Setting the name of a Thread

Mike Quilleash -2
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().
 
I thought I could just use a custom ThreadFactory and cast the Runnable but it seems the ThreadPool implementation wraps each Runnable up in a Worker inner class that I can't get my Runnable out of.
 
So I extended ThreadPoolExecutor and used the beforeExecute() hook which has my Runnable in it so I can cast out and change the thread name.  This worked fine.
 
Later on I decided to wrap up the Executor with a ExecutorCompletionService so I could trap any of these housekeeper tasks terminating early and report that in an error log.  But adding the CompletionService has now add another wrapper around my Runnable (the QueuedFuture that ECS uses) so my code in beforeExecute() broke with a ClassCastException.  I thought about storing the future result of ECS.submit -> my Runnable but it's then possible for the beforeExecute() to run before I've added the result to the map.
 
I ended up implementing ECS myself and storing a map of custom QueuedFuture -> Runnable and then doing the lookup in beforeExecute() to get my Runnable and therefore the thread name.
 
Is there any way or working around this as I've basically copied ECS out into my own code so I can create the FutureTask and add it to a map before it can be executed by the ThreadPool?
 
Any suggestions appreciated.
 

This e-mail is bound by the terms and conditions described at http://www.subexazure.com/mail-disclaimer.html


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Setting the name of a Thread

Ernst, Matthias
> 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

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Setting the name of a Thread

Mike Quilleash -2
In reply to this post by Mike Quilleash -2
Thanks.

That's a lot better and neater than what I had.  I've put the proper ECS
back in and it all works perfectly.

Cheers.

-----Original Message-----
From: Ernst, Matthias [mailto:[hidden email]]
Sent: 02 August 2006 16:25
To: Mike Quilleash ; [hidden email]
Subject: AW: [concurrency-interest] Setting the name of a Thread

> 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


 This e-mail is bound by the terms and conditions described at http://www.subexazure.com/mail-disclaimer.html


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest