Hay un open RFE para esto con Oracle. De los comentarios del empleado de Oracle parece que no entienden el problema y no lo arreglarán.Es una de estas cosas que es muy fácil de soportar en el JDK (sin romper la compatibilidad hacia atrás) por lo que es una pena que el RFE sea mal entendido.
Como se señaló, debe implementar su propio ThreadFactory. Si no desea obtener en Guava o Apache Commons solo para este propósito, proporciono aquí una implementación de ThreadFactory
que puede usar. Es exactamente similar a lo que obtienes del JDK, excepto por la capacidad de establecer el prefijo del nombre del hilo en algo más que "grupo".
package org.demo.concurrency;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* ThreadFactory with the ability to set the thread name prefix.
* This class is exactly similar to
* {@link java.util.concurrent.Executors#defaultThreadFactory()}
* from JDK8, except for the thread naming feature.
*
* <p>
* The factory creates threads that have names on the form
* <i>prefix-N-thread-M</i>, where <i>prefix</i>
* is a string provided in the constructor, <i>N</i> is the sequence number of
* this factory, and <i>M</i> is the sequence number of the thread created
* by this factory.
*/
public class ThreadFactoryWithNamePrefix implements ThreadFactory {
// Note: The source code for this class was based entirely on
// Executors.DefaultThreadFactory class from the JDK8 source.
// The only change made is the ability to configure the thread
// name prefix.
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
/**
* Creates a new ThreadFactory where threads are created with a name prefix
* of <code>prefix</code>.
*
* @param prefix Thread name prefix. Never use a value of "pool" as in that
* case you might as well have used
* {@link java.util.concurrent.Executors#defaultThreadFactory()}.
*/
public ThreadFactoryWithNamePrefix(String prefix) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup()
: Thread.currentThread().getThreadGroup();
namePrefix = prefix + "-"
+ poolNumber.getAndIncrement()
+ "-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon()) {
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}
Cuando se desea utilizarlo simplemente tomar ventaja del hecho de que todos los Executors
métodos permiten que proporcione su propia ThreadFactory
.
Este
Executors.newSingleThreadExecutor();
dará una ExecutorService donde las discusiones se nombran pool-N-thread-M
pero usando
Executors.newSingleThreadExecutor(new ThreadFactoryWithNamePrefix("primecalc");
obtendrá un ExecutorService donde las discusiones se nombran primecalc-N-thread-M
. Voila!
¡Eso es fantástico! –