package e.a.u.g;

import e.a.n;
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 n {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    static final C0117b f3793d;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    static final int f3795f = 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 f3796g;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    final AtomicReference<C0117b> f3798c;

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

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final e.a.u.a.d f3799b = new e.a.u.a.d();

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private final e.a.r.a f3800f = new e.a.r.a();

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private final e.a.u.a.d f3801g;

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

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

        a(c cVar) {
            this.f3802h = cVar;
            e.a.u.a.d dVar = new e.a.u.a.d();
            this.f3801g = dVar;
            dVar.c(this.f3799b);
            this.f3801g.c(this.f3800f);
        }

        @Override // e.a.n.c
        public e.a.r.b b(Runnable runnable) {
            return this.f3803i ? e.a.u.a.c.INSTANCE : this.f3802h.e(runnable, 0L, TimeUnit.MILLISECONDS, this.f3799b);
        }

        @Override // e.a.n.c
        public e.a.r.b c(Runnable runnable, long j2, TimeUnit timeUnit) {
            return this.f3803i ? e.a.u.a.c.INSTANCE : this.f3802h.e(runnable, j2, timeUnit, this.f3800f);
        }

        @Override // e.a.r.b
        public void dispose() {
            if (this.f3803i) {
                return;
            }
            this.f3803i = true;
            this.f3801g.dispose();
        }

        @Override // e.a.r.b
        public boolean isDisposed() {
            return this.f3803i;
        }
    }

    /* JADX INFO: renamed from: e.a.u.g.b$b, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ComputationScheduler.java */
    static final class C0117b {
        final int a;

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

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

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

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

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

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

    static {
        c cVar = new c(new f("RxComputationShutdown"));
        f3796g = cVar;
        cVar.dispose();
        f fVar = new f("RxComputationThreadPool", Math.max(1, Math.min(10, Integer.getInteger("rx2.computation-priority", 5).intValue())), true);
        f3794e = fVar;
        C0117b c0117b = new C0117b(0, fVar);
        f3793d = c0117b;
        c0117b.b();
    }

    public b() {
        this(f3794e);
    }

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

    @Override // e.a.n
    public n.c a() {
        return new a(this.f3798c.get().a());
    }

    @Override // e.a.n
    public e.a.r.b c(Runnable runnable, long j2, TimeUnit timeUnit) {
        return this.f3798c.get().a().f(runnable, j2, timeUnit);
    }

    @Override // e.a.n
    public e.a.r.b d(Runnable runnable, long j2, long j3, TimeUnit timeUnit) {
        return this.f3798c.get().a().g(runnable, j2, j3, timeUnit);
    }

    public void f() {
        C0117b c0117b = new C0117b(f3795f, this.f3797b);
        if (this.f3798c.compareAndSet(f3793d, c0117b)) {
            return;
        }
        c0117b.b();
    }

    public b(ThreadFactory threadFactory) {
        this.f3797b = threadFactory;
        this.f3798c = new AtomicReference<>(f3793d);
        f();
    }
}
