This is what I have so far:
public class Solution
{
public static AtomicInteger factoryNumber = new AtomicInteger(1);
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
{
public int factoryNumber = Solution.factoryNumber.getAndIncrement();
public AtomicInteger threadNumber = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r)
{
return new Thread(r,"GN-pool-" + factoryNumber + "-thread-" + threadNumber.getAndIncrement());
}
}
}
Which, when run, gives:
GN-pool-1-thread-1
GN-pool-1-thread-2
GN-pool-2-thread-1
GN-pool-2-thread-2
GN-pool-1-thread-3
GN-pool-2-thread-3
This looks enough like the task conditions output that I think I have accomplished the factoryNumber and threadNumber part.
But I'm pretty stuck on the GN piece. As far as within the main method, I could, for example, use thread2.getThreadGroup().getName() to get the piece of information I need for GN. But once the emulateThreadFactory() method is called, I lose that data. thread2 is not passed as information to that method, and since it's a static method I can't use this.getThreadGroup() either. By the time I enter that method, I no longer have access to which thread I entered it from.