package o.a.h.g;

import java.util.Objects;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import o.a.d;

/* JADX INFO: loaded from: classes.dex */
public final class b extends o.a.d {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final C0097b f6031b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final g f6032c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final int f6033d;

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

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

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

    public static final class a extends d.c {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final o.a.h.a.d f6037f;

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        public final o.a.e.a f6038m;

        /* JADX INFO: renamed from: n, reason: collision with root package name */
        public final o.a.h.a.d f6039n;

        /* JADX INFO: renamed from: o, reason: collision with root package name */
        public final c f6040o;

        /* JADX INFO: renamed from: p, reason: collision with root package name */
        public volatile boolean f6041p;

        public a(c cVar) {
            this.f6040o = cVar;
            o.a.h.a.d dVar = new o.a.h.a.d();
            this.f6037f = dVar;
            o.a.e.a aVar = new o.a.e.a();
            this.f6038m = aVar;
            o.a.h.a.d dVar2 = new o.a.h.a.d();
            this.f6039n = dVar2;
            dVar2.c(dVar);
            dVar2.c(aVar);
        }

        @Override // o.a.e.b
        public void b() {
            if (this.f6041p) {
                return;
            }
            this.f6041p = true;
            this.f6039n.b();
        }

        @Override // o.a.d.c
        public o.a.e.b c(Runnable runnable) {
            return this.f6041p ? o.a.h.a.c.INSTANCE : this.f6040o.g(runnable, 0L, TimeUnit.MILLISECONDS, this.f6037f);
        }

        @Override // o.a.d.c
        public o.a.e.b d(Runnable runnable, long j2, TimeUnit timeUnit) {
            return this.f6041p ? o.a.h.a.c.INSTANCE : this.f6040o.g(runnable, j2, timeUnit, this.f6038m);
        }
    }

    /* JADX INFO: renamed from: o.a.h.g.b$b, reason: collision with other inner class name */
    public static final class C0097b {
        public final int a;

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

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

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

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

    public static final class c extends f {
        public c(ThreadFactory threadFactory) {
            super(threadFactory);
        }
    }

    static {
        int iAvailableProcessors = Runtime.getRuntime().availableProcessors();
        int iIntValue = Integer.getInteger("rx2.computation-threads", 0).intValue();
        if (iIntValue > 0 && iIntValue <= iAvailableProcessors) {
            iAvailableProcessors = iIntValue;
        }
        f6033d = iAvailableProcessors;
        c cVar = new c(new g("RxComputationShutdown"));
        f6034e = cVar;
        cVar.b();
        g gVar = new g("RxComputationThreadPool", Math.max(1, Math.min(10, Integer.getInteger("rx2.computation-priority", 5).intValue())), true);
        f6032c = gVar;
        C0097b c0097b = new C0097b(0, gVar);
        f6031b = c0097b;
        for (c cVar2 : c0097b.f6042b) {
            cVar2.b();
        }
    }

    public b() {
        g gVar = f6032c;
        this.f6035f = gVar;
        C0097b c0097b = f6031b;
        AtomicReference<C0097b> atomicReference = new AtomicReference<>(c0097b);
        this.f6036g = atomicReference;
        C0097b c0097b2 = new C0097b(f6033d, gVar);
        if (atomicReference.compareAndSet(c0097b, c0097b2)) {
            return;
        }
        for (c cVar : c0097b2.f6042b) {
            cVar.b();
        }
    }

    @Override // o.a.d
    public d.c a() {
        return new a(this.f6036g.get().a());
    }

    @Override // o.a.d
    public o.a.e.b c(Runnable runnable, long j2, TimeUnit timeUnit) {
        c cVarA = this.f6036g.get().a();
        Objects.requireNonNull(cVarA);
        Objects.requireNonNull(runnable, "run is null");
        i iVar = new i(runnable);
        try {
            iVar.a(j2 <= 0 ? cVarA.f6071f.submit(iVar) : cVarA.f6071f.schedule(iVar, j2, timeUnit));
            return iVar;
        } catch (RejectedExecutionException e2) {
            g.h.a.i.P2(e2);
            return o.a.h.a.c.INSTANCE;
        }
    }

    @Override // o.a.d
    public o.a.e.b d(Runnable runnable, long j2, long j3, TimeUnit timeUnit) {
        c cVarA = this.f6036g.get().a();
        Objects.requireNonNull(cVarA);
        o.a.h.a.c cVar = o.a.h.a.c.INSTANCE;
        try {
            if (j3 <= 0) {
                o.a.h.g.c cVar2 = new o.a.h.g.c(runnable, cVarA.f6071f);
                cVar2.a(j2 <= 0 ? cVarA.f6071f.submit(cVar2) : cVarA.f6071f.schedule(cVar2, j2, timeUnit));
                return cVar2;
            }
            h hVar = new h(runnable);
            hVar.a(cVarA.f6071f.scheduleAtFixedRate(hVar, j2, j3, timeUnit));
            return hVar;
        } catch (RejectedExecutionException e2) {
            g.h.a.i.P2(e2);
            return cVar;
        }
    }
}
