package s.a; import d0.d0.f; import d0.g0.s; import java.lang.reflect.Method; import java.util.Objects; 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; import kotlin.coroutines.CoroutineContext; /* compiled from: CommonPool.kt */ public final class q extends t0 { public static final int i; public static final q j = new q(); public static volatile Executor pool; /* compiled from: CommonPool.kt */ public static final class a implements ThreadFactory { public final /* synthetic */ AtomicInteger i; public a(AtomicInteger atomicInteger) { this.i = atomicInteger; } @Override // java.util.concurrent.ThreadFactory public final Thread newThread(Runnable runnable) { StringBuilder L = c.d.b.a.a.L("CommonPool-worker-"); L.append(this.i.incrementAndGet()); Thread thread = new Thread(runnable, L.toString()); thread.setDaemon(true); return thread; } } static { String str; int i2; try { str = System.getProperty("kotlinx.coroutines.default.parallelism"); } catch (Throwable unused) { str = null; } if (str != null) { Integer intOrNull = s.toIntOrNull(str); if (intOrNull == null || intOrNull.intValue() < 1) { throw new IllegalStateException(c.d.b.a.a.u("Expected positive number in kotlinx.coroutines.default.parallelism, but has ", str).toString()); } i2 = intOrNull.intValue(); } else { i2 = -1; } i = i2; } public final ExecutorService H() { return Executors.newFixedThreadPool(K(), new a(new AtomicInteger())); } public final ExecutorService I() { Class cls; ExecutorService executorService; Integer num; if (System.getSecurityManager() != null) { return H(); } ExecutorService executorService2 = null; try { cls = Class.forName("java.util.concurrent.ForkJoinPool"); } catch (Throwable unused) { cls = null; } if (cls == null) { return H(); } if (i < 0) { try { Method method = cls.getMethod("commonPool", new Class[0]); Object invoke = method != null ? method.invoke(null, new Object[0]) : null; if (!(invoke instanceof ExecutorService)) { invoke = null; } executorService = (ExecutorService) invoke; } catch (Throwable unused2) { executorService = null; } if (executorService != null) { Objects.requireNonNull(j); executorService.submit(r.i); try { Object invoke2 = cls.getMethod("getPoolSize", new Class[0]).invoke(executorService, new Object[0]); if (!(invoke2 instanceof Integer)) { invoke2 = null; } num = (Integer) invoke2; } catch (Throwable unused3) { num = null; } if (!(num != null && num.intValue() >= 1)) { executorService = null; } if (executorService != null) { return executorService; } } } try { Object newInstance = cls.getConstructor(Integer.TYPE).newInstance(Integer.valueOf(j.K())); if (!(newInstance instanceof ExecutorService)) { newInstance = null; } executorService2 = (ExecutorService) newInstance; } catch (Throwable unused4) { } return executorService2 != null ? executorService2 : H(); } public final int K() { Integer valueOf = Integer.valueOf(i); if (!(valueOf.intValue() > 0)) { valueOf = null; } return valueOf != null ? valueOf.intValue() : f.coerceAtLeast(Runtime.getRuntime().availableProcessors() - 1, 1); } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() { throw new IllegalStateException("Close cannot be invoked on CommonPool".toString()); } @Override // s.a.z public void dispatch(CoroutineContext coroutineContext, Runnable runnable) { try { Executor executor = pool; if (executor == null) { synchronized (this) { executor = pool; if (executor == null) { executor = I(); pool = executor; } } } executor.execute(runnable); } catch (RejectedExecutionException unused) { c0.o.S(runnable); } } @Override // s.a.z public String toString() { return "CommonPool"; } }