package kotlinx.coroutines;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: compiled from: CommonPool.kt */
/* JADX INFO: loaded from: classes.dex */
public final class v extends k1 {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final v f1169f = new v();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private static final int f1170g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private static boolean f1171h;
    private static volatile Executor pool;

    static {
        String property;
        int iIntValue;
        try {
            property = System.getProperty("kotlinx.coroutines.default.parallelism");
        } catch (Throwable unused) {
            property = null;
        }
        if (property == null) {
            iIntValue = -1;
        } else {
            Integer numF = h.c0.o.f(property);
            if (numF == null || numF.intValue() < 1) {
                throw new IllegalStateException(h.x.d.k.i("Expected positive number in kotlinx.coroutines.default.parallelism, but has ", property).toString());
            }
            iIntValue = numF.intValue();
        }
        f1170g = iIntValue;
    }

    private v() {
    }

    private final ExecutorService K() {
        final AtomicInteger atomicInteger = new AtomicInteger();
        return Executors.newFixedThreadPool(R(), new ThreadFactory() { // from class: kotlinx.coroutines.a
            @Override // java.util.concurrent.ThreadFactory
            public final Thread newThread(Runnable runnable) {
                return v.N(atomicInteger, runnable);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final Thread N(AtomicInteger atomicInteger, Runnable runnable) {
        Thread thread = new Thread(runnable, h.x.d.k.i("CommonPool-worker-", Integer.valueOf(atomicInteger.incrementAndGet())));
        thread.setDaemon(true);
        return thread;
    }

    private final ExecutorService P() {
        Class<?> cls;
        Object objInvoke;
        if (System.getSecurityManager() != null) {
            return K();
        }
        ExecutorService executorService = null;
        try {
            cls = Class.forName("java.util.concurrent.ForkJoinPool");
        } catch (Throwable unused) {
            cls = null;
        }
        if (cls == null) {
            return K();
        }
        if (!f1171h && f1170g < 0) {
            try {
                objInvoke = cls.getMethod("commonPool", new Class[0]).invoke(null, new Object[0]);
            } catch (Throwable unused2) {
            }
            ExecutorService executorService2 = objInvoke instanceof ExecutorService ? (ExecutorService) objInvoke : null;
            if (executorService2 != null) {
                if (!f1169f.S(cls, executorService2)) {
                    executorService2 = null;
                }
                if (executorService2 != null) {
                    return executorService2;
                }
            }
        }
        try {
            Object objNewInstance = cls.getConstructor(Integer.TYPE).newInstance(Integer.valueOf(f1169f.R()));
            if (objNewInstance instanceof ExecutorService) {
                executorService = (ExecutorService) objNewInstance;
            }
        } catch (Throwable unused3) {
        }
        return executorService == null ? K() : executorService;
    }

    private final synchronized Executor Q() {
        Executor executorP;
        executorP = pool;
        if (executorP == null) {
            executorP = P();
            pool = executorP;
        }
        return executorP;
    }

    private final int R() {
        Integer numValueOf = Integer.valueOf(f1170g);
        if (!(numValueOf.intValue() > 0)) {
            numValueOf = null;
        }
        return numValueOf == null ? h.z.n.b(Runtime.getRuntime().availableProcessors() - 1, 1) : numValueOf.intValue();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void T() {
    }

    @Override // kotlinx.coroutines.i0
    public void H(h.u.g gVar, Runnable runnable) {
        try {
            Executor executorQ = pool;
            if (executorQ == null) {
                executorQ = Q();
            }
            d dVarA = e.a();
            executorQ.execute(dVarA == null ? runnable : dVarA.h(runnable));
        } catch (RejectedExecutionException unused) {
            d dVarA2 = e.a();
            if (dVarA2 != null) {
                dVarA2.e();
            }
            t0.f1168k.n0(runnable);
        }
    }

    public final boolean S(Class<?> cls, ExecutorService executorService) {
        executorService.submit(new Runnable() { // from class: kotlinx.coroutines.b
            @Override // java.lang.Runnable
            public final void run() {
                v.T();
            }
        });
        Integer num = null;
        try {
            Object objInvoke = cls.getMethod("getPoolSize", new Class[0]).invoke(executorService, new Object[0]);
            if (objInvoke instanceof Integer) {
                num = (Integer) objInvoke;
            }
        } catch (Throwable unused) {
        }
        return num != null && num.intValue() >= 1;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        throw new IllegalStateException("Close cannot be invoked on CommonPool".toString());
    }

    @Override // kotlinx.coroutines.i0
    public String toString() {
        return "CommonPool";
    }
}
