[concurrency-interest] Setting the name of a Thread

Mike Quilleash mike.quilleash at subexazure.com
Wed Aug 2 10:59:57 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().
 
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060802/10e7e441/attachment.html 


More information about the Concurrency-interest mailing list