I am executing a Callable Object using ExecutorService thread pool. I want to give a name to this thread.
To be more specific, in older version I did this -
Thread thread = new Thread(runnable Task);
thread.setName("My Thread Name");
private final ExecutorService executorPool = Executors.newCachedThreadPool();
Future<String> result = executorPool.submit(callable Task);
You may use the overloaded method:
which allows you to pass a
that should allow you to set the thread's names via
Constructs a new
Thread. Implementations may also initialize priority, name, daemon status,
Have a look at
java.util.concurrent.Executors.DefaultThreadFactory for a default implementation.
Since I see that this thread is still visited, Guava (if you have it available) provides a ThreadFactoryBuilder that leverages the need of having an inner anonymous class and even allows for customizing parametrized names for your threads.