package io.netty.util.concurrent;

import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.StringUtil;
import java.util.Locale;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes.dex */
public class DefaultThreadFactory implements ThreadFactory {
    private static final AtomicInteger poolId = new AtomicInteger();
    private final boolean daemon;
    private final AtomicInteger nextId;
    private final String prefix;
    private final int priority;
    protected final ThreadGroup threadGroup;

    private static final class DefaultRunnableDecorator implements Runnable {
        private final Runnable r;

        DefaultRunnableDecorator(Runnable runnable) {
            this.r = runnable;
        }

        @Override // java.lang.Runnable
        public void run() {
            try {
                this.r.run();
            } finally {
                FastThreadLocal.removeAll();
            }
        }
    }

    public DefaultThreadFactory(Class<?> cls) {
        this(cls, false, 5);
    }

    private static String toPoolName(Class<?> cls) {
        if (cls == null) {
            throw new NullPointerException("poolType");
        }
        String strSimpleClassName = StringUtil.simpleClassName(cls);
        int length = strSimpleClassName.length();
        if (length == 0) {
            return "unknown";
        }
        if (length == 1) {
            return strSimpleClassName.toLowerCase(Locale.US);
        }
        if (!Character.isUpperCase(strSimpleClassName.charAt(0)) || !Character.isLowerCase(strSimpleClassName.charAt(1))) {
            return strSimpleClassName;
        }
        return Character.toLowerCase(strSimpleClassName.charAt(0)) + strSimpleClassName.substring(1);
    }

    @Override // java.util.concurrent.ThreadFactory
    public Thread newThread(Runnable runnable) {
        Thread threadNewThread = newThread(new DefaultRunnableDecorator(runnable), this.prefix + this.nextId.incrementAndGet());
        try {
            if (threadNewThread.isDaemon()) {
                if (!this.daemon) {
                    threadNewThread.setDaemon(false);
                }
            } else if (this.daemon) {
                threadNewThread.setDaemon(true);
            }
            if (threadNewThread.getPriority() != this.priority) {
                threadNewThread.setPriority(this.priority);
            }
        } catch (Exception unused) {
        }
        return threadNewThread;
    }

    public DefaultThreadFactory(String str) {
        this(str, false, 5);
    }

    public DefaultThreadFactory(Class<?> cls, boolean z) {
        this(cls, z, 5);
    }

    public DefaultThreadFactory(String str, boolean z) {
        this(str, z, 5);
    }

    public DefaultThreadFactory(Class<?> cls, int i2) {
        this(cls, false, i2);
    }

    public DefaultThreadFactory(String str, int i2) {
        this(str, false, i2);
    }

    public DefaultThreadFactory(Class<?> cls, boolean z, int i2) {
        this(toPoolName(cls), z, i2);
    }

    public DefaultThreadFactory(String str, boolean z, int i2, ThreadGroup threadGroup) {
        this.nextId = new AtomicInteger();
        if (str == null) {
            throw new NullPointerException("poolName");
        }
        if (i2 >= 1 && i2 <= 10) {
            this.prefix = str + '-' + poolId.incrementAndGet() + '-';
            this.daemon = z;
            this.priority = i2;
            this.threadGroup = (ThreadGroup) ObjectUtil.checkNotNull(threadGroup, "threadGroup");
            return;
        }
        throw new IllegalArgumentException("priority: " + i2 + " (expected: Thread.MIN_PRIORITY <= priority <= Thread.MAX_PRIORITY)");
    }

    protected Thread newThread(Runnable runnable, String str) {
        return new FastThreadLocalThread(this.threadGroup, runnable, str);
    }

    public DefaultThreadFactory(String str, boolean z, int i2) {
        this(str, z, i2, Thread.currentThread().getThreadGroup());
    }
}
