package i.a.n.g;

import i.a.h;
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 i.a.h {
    static final C0079b d;
    static final g e;
    static final int f = e(Runtime.getRuntime().availableProcessors(), Integer.getInteger("rx2.computation-threads", 0).intValue());

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    static final c f867g;
    final ThreadFactory b;
    final AtomicReference<C0079b> c;

    /* JADX INFO: compiled from: ComputationScheduler.java */
    static final class a extends h.c {
        private final i.a.n.a.d b = new i.a.n.a.d();
        private final i.a.k.a d = new i.a.k.a();
        private final i.a.n.a.d e;
        private final c f;

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

        a(c cVar) {
            this.f = cVar;
            i.a.n.a.d dVar = new i.a.n.a.d();
            this.e = dVar;
            dVar.c(this.b);
            this.e.c(this.d);
        }

        @Override // i.a.h.c
        public i.a.k.b b(Runnable runnable) {
            return this.f868g ? i.a.n.a.c.INSTANCE : this.f.e(runnable, 0L, TimeUnit.MILLISECONDS, this.b);
        }

        @Override // i.a.h.c
        public i.a.k.b c(Runnable runnable, long j2, TimeUnit timeUnit) {
            return this.f868g ? i.a.n.a.c.INSTANCE : this.f.e(runnable, j2, timeUnit, this.d);
        }

        @Override // i.a.k.b
        public void dispose() {
            if (this.f868g) {
                return;
            }
            this.f868g = true;
            this.e.dispose();
        }

        @Override // i.a.k.b
        public boolean isDisposed() {
            return this.f868g;
        }
    }

    /* JADX INFO: renamed from: i.a.n.g.b$b, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ComputationScheduler.java */
    static final class C0079b {
        final int a;
        final c[] b;
        long c;

        C0079b(int i2, ThreadFactory threadFactory) {
            this.a = i2;
            this.b = new c[i2];
            for (int i3 = 0; i3 < i2; i3++) {
                this.b[i3] = new c(threadFactory);
            }
        }

        public c a() {
            int i2 = this.a;
            if (i2 == 0) {
                return b.f867g;
            }
            c[] cVarArr = this.b;
            long j2 = this.c;
            this.c = 1 + j2;
            return cVarArr[(int) (j2 % ((long) i2))];
        }

        public void b() {
            for (c cVar : this.b) {
                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 g("RxComputationShutdown"));
        f867g = cVar;
        cVar.dispose();
        g gVar = new g("RxComputationThreadPool", Math.max(1, Math.min(10, Integer.getInteger("rx2.computation-priority", 5).intValue())), true);
        e = gVar;
        C0079b c0079b = new C0079b(0, gVar);
        d = c0079b;
        c0079b.b();
    }

    public b() {
        this(e);
    }

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

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

    @Override // i.a.h
    public i.a.k.b c(Runnable runnable, long j2, TimeUnit timeUnit) {
        return this.c.get().a().f(runnable, j2, timeUnit);
    }

    @Override // i.a.h
    public i.a.k.b d(Runnable runnable, long j2, long j3, TimeUnit timeUnit) {
        return this.c.get().a().g(runnable, j2, j3, timeUnit);
    }

    public void f() {
        C0079b c0079b = new C0079b(f, this.b);
        if (this.c.compareAndSet(d, c0079b)) {
            return;
        }
        c0079b.b();
    }

    public b(ThreadFactory threadFactory) {
        this.b = threadFactory;
        this.c = new AtomicReference<>(d);
        f();
    }
}
