Не проходит по пункту шаблона, хотя при тесте все нормально получается.
package com.javarush.task.task28.task2802;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/*
Пишем свою ThreadFactory
*/
public class Solution {
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 {
ThreadGroup group;
AtomicInteger poolNumber = new AtomicInteger(1);
AtomicInteger threadNumber = new AtomicInteger(1);
String namePrefix;
AmigoThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null)? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = group.getName() + "-pool-" +
threadNumber.getAndIncrement() +
"-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(group, r, namePrefix + poolNumber.getAndIncrement(),0);
if (thread.isDaemon())
thread.setDaemon(false);
if (thread.getPriority() != Thread.NORM_PRIORITY)
thread.setPriority(Thread.NORM_PRIORITY);
// thread.setName("GN-pool-A-thread-B");
return thread;
}
}
}