Помогите понять проблему. С виду - вроде вывод верный. Может чего-то не догоняю..
package com.javarush.task.task28.task2802;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/*
Пишем свою ThreadFactory
*/
public class Solution {
static AtomicInteger A = new AtomicInteger(0);
public static void main(String[] args) {
class EmulatorThreadFactoryTask implements Runnable {
@Override
public void run() {
emulateThreadFactory();
}
}
ThreadGroup group = new ThreadGroup("firstGroup");
Thread thread = new Thread(group, new EmulatorThreadFactoryTask());
ThreadGroup group2 = new ThreadGroup("secondGroup");
Thread thread2 = new Thread(group2, new EmulatorThreadFactoryTask());
thread.start();
thread2.start();
}
private static void emulateThreadFactory() {
AmigoThreadFactory factory = new AmigoThreadFactory();
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
};
factory.newThread(r).start();
factory.newThread(r).start();
factory.newThread(r).start();
}
public static class AmigoThreadFactory implements ThreadFactory {
AtomicInteger B = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
Thread newThread = new Thread(threadGroup, r);
newThread.setDaemon(false);
newThread.setPriority(Thread.NORM_PRIORITY);
newThread.setName(String.format("%s-pool-%d-thread-%d",
threadGroup.getName(), Solution.A.incrementAndGet(), B.incrementAndGet()));
return newThread;
}
}
}