[concurrency-interest] DefaultThreadFactory implementation questions

Roel Spilker R.Spilker at topdesk.com
Wed Jan 14 12:15:08 EST 2009


Tim,
 
Regarding (1):
 
The constructor public Thread(Runnable target, String name) already uses the ThreadGroup of the SecurityManager by providing null as the ThreadGroup to the init method.
 
Regarding (2):
 
I agree you could do it like that. However, I think it would still be a good idea to make it easy for the users to create a default thread factory with a given namePrefix. If we would follow your reasoning, there is also no need for public static ExecutorService newFixedThreadPool(int nThreads), since the caller could call public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) after getting threadFactory using Executors. defaultThreadFactory(). 
 
If "folks would disagree on the precise format", they could always choose to use a decorator. But providing a default implementation that enhances debugging and monitoring consists of a simple API change, and requires only a few lines of code to be changed. I think that would be good value for money.
 
Roel
 

 
Van: tim at peierls.net [mailto:tpeierls at gmail.com] Namens Tim Peierls
Verzonden: woensdag 14 januari 2009 17:51
Aan: Roel Spilker
CC: concurrency-interest
Onderwerp: Re: [concurrency-interest] DefaultThreadFactory implementation questions



Regarding (2), this doesn't seem like something that should be in the standard library, since folks would disagree on the precise format of the prefixed thread name and others would want custom *suffixes*. And it's very easy to write your own ThreadFactory decorator that arranges for custom thread name prefixing:


 public static ThreadFactory prefixedThreadName(final ThreadFactory threadFactory, final String prefix) {
     return new ThreadFactory() {

         public Thread newThread(Runnable runnable) {
             Thread t = threadFactory.newThread(runnable);
             t.setName(prefix + t.getName()); // or with a hyphen, or replacing "pool" with prefix, or ...
             return t;
         }
     };
 }


 ...


 ThreadFactory myThreadFactory = prefixedThreadName(defaultThreadFactory(), "myprefix");


The explicit use of ThreadGroup in the DefaultThreadFactory constructor is to make sure that if a SecurityManager exists then threads created by the factory will be in that SecurityManager's thread group. This might only be relevant to the PrivilegedThreadFactory subclass.



--tim

  




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20090114/4e0e9078/attachment.html>


More information about the Concurrency-interest mailing list