package c.a.q.g;

import c.a.g;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: compiled from: ComputationScheduler.java */
/* JADX INFO: loaded from: classes.dex */
public final class b extends c.a.g {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    static final C0071b f1825b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    static final h f1826c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    static final int f1827d = e(Runtime.getRuntime().availableProcessors(), Integer.getInteger("rx2.computation-threads", 0).intValue());

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    static final c f1828e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    final ThreadFactory f1829f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    final AtomicReference<C0071b> f1830g;

    /* JADX INFO: compiled from: ComputationScheduler.java */
    static final class a extends g.c {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private final c.a.q.a.d f1831c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private final c.a.n.a f1832d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private final c.a.q.a.d f1833e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private final c f1834f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        volatile boolean f1835g;

        a(c cVar) {
            this.f1834f = cVar;
            c.a.q.a.d dVar = new c.a.q.a.d();
            this.f1831c = dVar;
            c.a.n.a aVar = new c.a.n.a();
            this.f1832d = aVar;
            c.a.q.a.d dVar2 = new c.a.q.a.d();
            this.f1833e = dVar2;
            dVar2.b(dVar);
            dVar2.b(aVar);
        }

        @Override // c.a.g.c
        public c.a.n.b b(Runnable runnable) {
            return this.f1835g ? c.a.q.a.c.INSTANCE : this.f1834f.e(runnable, 0L, TimeUnit.MILLISECONDS, this.f1831c);
        }

        @Override // c.a.g.c
        public c.a.n.b c(Runnable runnable, long j, TimeUnit timeUnit) {
            return this.f1835g ? c.a.q.a.c.INSTANCE : this.f1834f.e(runnable, j, timeUnit, this.f1832d);
        }

        @Override // c.a.n.b
        public void dispose() {
            if (this.f1835g) {
                return;
            }
            this.f1835g = true;
            this.f1833e.dispose();
        }
    }

    /* JADX INFO: renamed from: c.a.q.g.b$b, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ComputationScheduler.java */
    static final class C0071b {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final int f1836a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final c[] f1837b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        long f1838c;

        C0071b(int i, ThreadFactory threadFactory) {
            this.f1836a = i;
            this.f1837b = new c[i];
            for (int i2 = 0; i2 < i; i2++) {
                this.f1837b[i2] = new c(threadFactory);
            }
        }

        public c a() {
            int i = this.f1836a;
            if (i == 0) {
                return b.f1828e;
            }
            c[] cVarArr = this.f1837b;
            long j = this.f1838c;
            this.f1838c = 1 + j;
            return cVarArr[(int) (j % ((long) i))];
        }

        public void b() {
            for (c cVar : this.f1837b) {
                cVar.dispose();
            }
        }
    }

    /* JADX INFO: compiled from: ComputationScheduler.java */
    static final class c extends f {
        c(ThreadFactory threadFactory) {
            super(threadFactory);
        }
    }

    static {
        c cVar = new c(new h("RxComputationShutdown"));
        f1828e = cVar;
        cVar.dispose();
        h hVar = new h("RxComputationThreadPool", Math.max(1, Math.min(10, Integer.getInteger("rx2.computation-priority", 5).intValue())), true);
        f1826c = hVar;
        C0071b c0071b = new C0071b(0, hVar);
        f1825b = c0071b;
        c0071b.b();
    }

    public b() {
        this(f1826c);
    }

    static int e(int i, int i2) {
        return (i2 <= 0 || i2 > i) ? i : i2;
    }

    @Override // c.a.g
    public g.c a() {
        return new a(this.f1830g.get().a());
    }

    @Override // c.a.g
    public c.a.n.b c(Runnable runnable, long j, TimeUnit timeUnit) {
        return this.f1830g.get().a().f(runnable, j, timeUnit);
    }

    @Override // c.a.g
    public c.a.n.b d(Runnable runnable, long j, long j2, TimeUnit timeUnit) {
        return this.f1830g.get().a().g(runnable, j, j2, timeUnit);
    }

    public void f() {
        C0071b c0071b = new C0071b(f1827d, this.f1829f);
        if (this.f1830g.compareAndSet(f1825b, c0071b)) {
            return;
        }
        c0071b.b();
    }

    public b(ThreadFactory threadFactory) {
        this.f1829f = threadFactory;
        this.f1830g = new AtomicReference<>(f1825b);
        f();
    }
}
