Há algum motivo especial pelo qual não sincronizamos de alguma forma (synchronized, AtomicInteger, etc) o acesso ao:
private static int numero = 1;
da nossa FabricaDeThreads?
public class FabricaDeThreads implements ThreadFactory {
private static int numero = 1;
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "Thread Servidor Tarefas " + numero);
numero++;
thread.setUncaughtExceptionHandler(new TratadorDeExcecao());
return thread;
}
}
Sendo que o próprio DefaultThreadFactory do Java assim o faz:
private static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
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;
}
}
Obrigado.