package d.a.s.g;

import d.a.m;
import java.util.Objects;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

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

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

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

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

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

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final AtomicReference<a> f5846i;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final TimeUnit f5842e = TimeUnit.SECONDS;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final long f5841d = Long.getLong("rx2.io-keep-alive-time", 60).longValue();

    public static final class a implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final long f5847b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final ConcurrentLinkedQueue<c> f5848c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final d.a.p.a f5849d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final ScheduledExecutorService f5850e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final Future<?> f5851f;

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

        public a(long j2, TimeUnit timeUnit, ThreadFactory threadFactory) {
            ScheduledFuture<?> scheduledFutureScheduleWithFixedDelay;
            long nanos = timeUnit != null ? timeUnit.toNanos(j2) : 0L;
            this.f5847b = nanos;
            this.f5848c = new ConcurrentLinkedQueue<>();
            this.f5849d = new d.a.p.a();
            this.f5852g = threadFactory;
            ScheduledExecutorService scheduledExecutorServiceNewScheduledThreadPool = null;
            if (timeUnit != null) {
                scheduledExecutorServiceNewScheduledThreadPool = Executors.newScheduledThreadPool(1, d.f5840c);
                scheduledFutureScheduleWithFixedDelay = scheduledExecutorServiceNewScheduledThreadPool.scheduleWithFixedDelay(this, nanos, nanos, TimeUnit.NANOSECONDS);
            } else {
                scheduledFutureScheduleWithFixedDelay = null;
            }
            this.f5850e = scheduledExecutorServiceNewScheduledThreadPool;
            this.f5851f = scheduledFutureScheduleWithFixedDelay;
        }

        @Override // java.lang.Runnable
        public void run() {
            if (this.f5848c.isEmpty()) {
                return;
            }
            long jNanoTime = System.nanoTime();
            for (c cVar : this.f5848c) {
                if (cVar.f5857d > jNanoTime) {
                    return;
                }
                if (this.f5848c.remove(cVar) && this.f5849d.a(cVar)) {
                    cVar.dispose();
                }
            }
        }
    }

    public static final class b extends m.c {

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

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

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final AtomicBoolean f5856e = new AtomicBoolean();

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final d.a.p.a f5853b = new d.a.p.a();

        public b(a aVar) {
            c cVar;
            c cVar2;
            this.f5854c = aVar;
            if (aVar.f5849d.f5663c) {
                cVar2 = d.f5843f;
            } else {
                while (true) {
                    if (aVar.f5848c.isEmpty()) {
                        cVar = new c(aVar.f5852g);
                        aVar.f5849d.c(cVar);
                        break;
                    } else {
                        cVar = aVar.f5848c.poll();
                        if (cVar != null) {
                            break;
                        }
                    }
                }
                cVar2 = cVar;
            }
            this.f5855d = cVar2;
        }

        @Override // d.a.m.c
        public d.a.p.b c(Runnable runnable, long j2, TimeUnit timeUnit) {
            return this.f5853b.f5663c ? d.a.s.a.c.INSTANCE : this.f5855d.e(runnable, j2, timeUnit, this.f5853b);
        }

        @Override // d.a.p.b
        public void dispose() {
            if (this.f5856e.compareAndSet(false, true)) {
                this.f5853b.dispose();
                a aVar = this.f5854c;
                c cVar = this.f5855d;
                Objects.requireNonNull(aVar);
                cVar.f5857d = System.nanoTime() + aVar.f5847b;
                aVar.f5848c.offer(cVar);
            }
        }

        @Override // d.a.p.b
        public boolean isDisposed() {
            return this.f5856e.get();
        }
    }

    public static final class c extends f {

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

        public c(ThreadFactory threadFactory) {
            super(threadFactory);
            this.f5857d = 0L;
        }
    }

    static {
        c cVar = new c(new g("RxCachedThreadSchedulerShutdown"));
        f5843f = cVar;
        cVar.dispose();
        int iMax = Math.max(1, Math.min(10, Integer.getInteger("rx2.io-priority", 5).intValue()));
        g gVar = new g("RxCachedThreadScheduler", iMax);
        f5839b = gVar;
        f5840c = new g("RxCachedWorkerPoolEvictor", iMax);
        a aVar = new a(0L, null, gVar);
        f5844g = aVar;
        aVar.f5849d.dispose();
        Future<?> future = aVar.f5851f;
        if (future != null) {
            future.cancel(true);
        }
        ScheduledExecutorService scheduledExecutorService = aVar.f5850e;
        if (scheduledExecutorService != null) {
            scheduledExecutorService.shutdownNow();
        }
    }

    public d() {
        g gVar = f5839b;
        this.f5845h = gVar;
        a aVar = f5844g;
        AtomicReference<a> atomicReference = new AtomicReference<>(aVar);
        this.f5846i = atomicReference;
        a aVar2 = new a(f5841d, f5842e, gVar);
        if (atomicReference.compareAndSet(aVar, aVar2)) {
            return;
        }
        aVar2.f5849d.dispose();
        Future<?> future = aVar2.f5851f;
        if (future != null) {
            future.cancel(true);
        }
        ScheduledExecutorService scheduledExecutorService = aVar2.f5850e;
        if (scheduledExecutorService != null) {
            scheduledExecutorService.shutdownNow();
        }
    }

    @Override // d.a.m
    public m.c a() {
        return new b(this.f5846i.get());
    }
}
