logo       

Re: Setting a Thread name in a ThreadPool: msg#00063

java.jsr.166-concurrency

Subject: Re: Setting a Thread name in a ThreadPool

Hi Mike,

We use a slightly more nuanced ThreadFactory to give unique names to
each thread.

class CountingThreadFactory extends ThreadFactory {
private static final AtomicInteger count = new AtomicInteger(0);
public Thread newThread(Runnable r) {
return new Thread(r, "thread" + count.getAndIncrement());
}
}

Regards,
Kevin

On 10/26/06, Mike Quilleash <mike.quilleash@xxxxxxxxxxxxxx> wrote:
> This is simpler if you just need a single name for all threads in the
> pool. My requirement at the time was for a different name per runnable
> so I had to do it differently.
>
> Cheers.
>
> -----Original Message-----
> From: Kevin Condon [mailto:conivek@xxxxxxxxx]
> Sent: 26 October 2006 14:05
> To: Mike Quilleash
> Cc: David Harrigan; concurrency-interest@xxxxxxxxxxxxx
> Subject: Re: [concurrency-interest] Setting a Thread name in a
> ThreadPool
>
> You can use:
>
> ThreadFactory factory = new ThreadFactory() {
> public Thread newThread(Runnable r) {
> return new Thread(r, "threadname");
> }
> };
> Executors.newCachedThreadPool(factory);
>
> Kevin
>
> On 10/26/06, Mike Quilleash <mike.quilleash@xxxxxxxxxxxxxx> wrote:
> > I asked the same question a while back. The solution for me was to
> > wrap up whatever you are submitting to the executor with a Runnable.
> >
> > Something like
> >
> > Public class ThreadRenameWrapper implements Runnable { private final
> > String name; private final Runnable runnable;
> >
> > public ThreadRenameWrapper( String name, Runnable runnable ) {
> > this.name = name;
> > this.runnable = runnable;
> > }
> >
> > public void run()
> > {
> > Thread thread = Thread.currentThread();
> > String oldName = thread.getName();
> >
> > thread.setName( name );
> > try
> > {
> > runnable.run();
> > }
> > finally
> > {
> > thread.setName( oldName );
> > }
> > }
> > }
> >
> > HTH.
> >
> > -----Original Message-----
> > From: concurrency-interest-bounces@xxxxxxxxxxxxx
> > [mailto:concurrency-interest-bounces@xxxxxxxxxxxxx] On Behalf Of David
>
> > Harrigan
> > Sent: 26 October 2006 13:13
> > To: concurrency-interest@xxxxxxxxxxxxx
> > Subject: [concurrency-interest] Setting a Thread name in a ThreadPool
> >
> >
> > Hi,
> >
> > Previously, if I wanted to set a thread name, I could so something
> > like
> > this:
> >
> > new Thread(new RunnableTask(), "DoSomethingWonderfulThread").start();
> >
> > now, if I'm delgating over to an Executors, like
> > Executors.newCachedThreadPool(), and using that to execute my thread:
> >
> > ExecutorService s = Executors.newCachedThreadPool(); s.execute(new
> > RunnableTask());
> >
> > The execute method isn't overloaded to provide a name, so, how do I go
>
> > about setting the Thread name?
> >
> > -=david=-
> > --
> > View this message in context:
> > http://www.nabble.com/Setting-a-Thread-name-in-a-ThreadPool-tf2513566.
> > ht
> > ml#a7009924
> > Sent from the JSR166 Concurrency mailing list archive at Nabble.com.
> >
> > _______________________________________________
> > Concurrency-interest mailing list
> > Concurrency-interest@xxxxxxxxxxxxxxxxxxxx
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> >
> >
> > This e-mail is bound by the terms and conditions described at
> > http://www.subexazure.com/mail-disclaimer.html
> >
> >
> > _______________________________________________
> > Concurrency-interest mailing list
> > Concurrency-interest@xxxxxxxxxxxxxxxxxxxx
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> >
>
>
> This e-mail is bound by the terms and conditions described at
> http://www.subexazure.com/mail-disclaimer.html
>
>


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise