views:

90

answers:

2

If I am extending an existing ThreadFactory implementation, how would I go able ensuring that all threads created would have the same prefix? I'm sick and tired of looking at Thread-9 in my logs and would like to distinguish between threads more easily.

Does anyone have any suggestions as to how to go about this?

+4  A: 

Provide your own implementation of the ThreadFactory interface:

pool = Executors.newScheduledThreadPool(numberOfThreads, new TF());

class TF implements ThreadFactory {
    public synchronized Thread newThread(Runnable r) {
        Thread t = new Thread(r) ;
        t.setName("Something here....");  
        return t;
    }
}
Visage
Thanks for the example. However, the API I am using only allows me to specify the ThreadPool class implementation and not the ThreadFactory implementation.
Elijah
What do you mean by ThreadPool class ? Which interface do you have to work with ? Executor ?
Brian Agnew
Wow. I feel stupid. ThreadPool is an interface in Quartz. Thank you for your answer. I think it will be useful. I'm going to update the question.
Elijah
A: 

Can you provide your own ThreadFactory ? That will allow you to create threads with whatever naming convention you require.

Brian Agnew