secondGroup-pool-2-thread-2
secondGroup-pool-2-thread-1
secondGroup-pool-2-thread-3
firstGroup-pool-1-thread-1
firstGroup-pool-1-thread-2
firstGroup-pool-1-thread-3
package com.javarush.task.task28.task2802;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/*
Пишем свою ThreadFactory
*/
public class Solution {
public static class AmigoThreadFactory implements ThreadFactory{
AtomicInteger factoryNum=new AtomicInteger(0);
static AtomicInteger factoryCount=new AtomicInteger(0);
public AmigoThreadFactory() {
factoryNum.set(factoryCount.incrementAndGet());
}
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(false);
thread.setPriority(Thread.NORM_PRIORITY);
String Gn = Thread.currentThread().getThreadGroup().getName();
int gNumber = Thread.currentThread().getThreadGroup().activeGroupCount();
int tNumber = Thread.currentThread().getThreadGroup().activeCount();
StringBuilder sBuilder = new StringBuilder().append(Gn).append("-pool-").append(factoryNum).append("-thread-").append(tNumber);
thread.setName(sBuilder.toString());
return thread;
}
}
public static void main(String[] args) {
class EmulateThreadFactoryTask implements Runnable {
@Override
public void run() {
emulateThreadFactory();
}
}
ThreadGroup group = new ThreadGroup("firstGroup");
Thread thread = new Thread(group, new EmulateThreadFactoryTask());
ThreadGroup group2 = new ThreadGroup("secondGroup");
Thread thread2 = new Thread(group2, new EmulateThreadFactoryTask());
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();
}
}