package com.its.utils.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class NamedThreadFactory implements ThreadFactory {

    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final ThreadGroup group;
    private final String namePrefix;

    public NamedThreadFactory(String name) {
        this.group = new ThreadGroup(name);
        this.namePrefix = (group.getName() + "-");
    }

    public Thread newThread(Runnable r) {
        return new Thread(this.group, r, this.namePrefix + this.threadNumber.getAndIncrement(), 0L);
    }

    public Thread newThread(Runnable r, String title) {
        return new Thread(this.group, r, this.namePrefix + this.threadNumber.getAndIncrement() + title, 0L);
    }
}
