[concurrency-interest] DefaultThreadFactory implementation questions

Joe Bowbeer joe.bowbeer at gmail.com
Wed Jan 14 12:00:13 EST 2009

All of the Executors thread pool factory methods have two forms: one that
doesn't take a custom thread factory, and one that does.  The hope was that
anyone who had special thread-creation needs would use the second form,
providing their own thread factory.

If, for example, you want to specify your own prefix, you can wrap the
defaultThreadFactory() of Executors in your own ThreadFactory, as Tim
illustrates, and then specify this custom thread factory when you create
your thread pool.  OK?

(I think custom names is a great reason to provide your own thread factory.)


On Wed, Jan 14, 2009 at 7:52 AM, Roel Spilker wrote:

>  L.S.,
> In the class j.u.c.Executors, there is a package private static class
> DefaultThreadFactory. This class synthesizes a name for all created threads.
> For debugging and monitoring purposes, I would like to be able to provide a
> custom namePrefix. This has lead me to two questions:
> 1) Is there a reason the ThreadGroup is determined in the constructor and
> the later used to create a new Thread? As far as I can see, Thread also has
> a constructor that takes a Runnable and a String name that would use similar
> code to determine the ThreadGroup.
> 2) Do other people also feel the need to provide a name? Or is there
> another way to provide more debugging and monitoring information? If so, is
> it appropriate to add some API to do so?
> I think the API changes could be small. The smallest change would be to add
> two static methods to Executors:
> public static ThreadFactory defaultThreadFactory(String namePrefix);
> public static ThreadFactory privilagedThreadFactory(String namePrefix);
> This would be enough, since the code in j.u.g that call defaultThreadFactory
> also have a counterpart that accepts a provided ThreadFactory.
> A more complete API would be to also add:
> public static ExecutorService newFixedThreadPool(int nThreads, String
> namePrefix);
> public static ExecutorService newSingleThreadExecutor(String namePrefix);
> public static ExecutorService newCachedThreadPool(String namePrefix);
> ... and probably the same for the ScheduledExecutorService factory methods.
> Also, ThreadPoolExecutor could get two extra constructors that accept a
> namePrefix.
> The changes in the code would, for as far as I can see, be very small.
> Roel Spilker
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20090114/ce1b02fb/attachment.html>

More information about the Concurrency-interest mailing list