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 f5854b;

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

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

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

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final long f5856d = 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 f5862b;

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

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

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

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

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

        public a(long j2, TimeUnit timeUnit, ThreadFactory threadFactory) {
            ScheduledFuture<?> scheduledFutureScheduleWithFixedDelay;
            long nanos = timeUnit != null ? timeUnit.toNanos(j2) : 0L;
            this.f5862b = nanos;
            this.f5863c = new ConcurrentLinkedQueue<>();
            this.f5864d = new d.a.p.a();
            this.f5867g = threadFactory;
            ScheduledExecutorService scheduledExecutorServiceNewScheduledThreadPool = null;
            if (timeUnit != null) {
                scheduledExecutorServiceNewScheduledThreadPool = Executors.newScheduledThreadPool(1, d.f5855c);
                scheduledFutureScheduleWithFixedDelay = scheduledExecutorServiceNewScheduledThreadPool.scheduleWithFixedDelay(this, nanos, nanos, TimeUnit.NANOSECONDS);
            } else {
                scheduledFutureScheduleWithFixedDelay = null;
            }
            this.f5865e = scheduledExecutorServiceNewScheduledThreadPool;
            this.f5866f = scheduledFutureScheduleWithFixedDelay;
        }

        @Override // java.lang.Runnable
        public void run() {
            if (this.f5863c.isEmpty()) {
                return;
            }
            long jNanoTime = System.nanoTime();
            for (c cVar : this.f5863c) {
                if (cVar.f5872d > jNanoTime) {
                    return;
                }
                if (this.f5863c.remove(cVar) && this.f5864d.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 f5869c;

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

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

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

        public b(a aVar) {
            c cVar;
            c cVar2;
            this.f5869c = aVar;
            if (aVar.f5864d.f5678c) {
                cVar2 = d.f5858f;
            } else {
                while (true) {
                    if (aVar.f5863c.isEmpty()) {
                        cVar = new c(aVar.f5867g);
                        aVar.f5864d.c(cVar);
                        break;
                    } else {
                        cVar = aVar.f5863c.poll();
                        if (cVar != null) {
                            break;
                        }
                    }
                }
                cVar2 = cVar;
            }
            this.f5870d = cVar2;
        }

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

        @Override // d.a.p.b
        public void dispose() {
            if (this.f5871e.compareAndSet(false, true)) {
                this.f5868b.dispose();
                a aVar = this.f5869c;
                c cVar = this.f5870d;
                Objects.requireNonNull(aVar);
                cVar.f5872d = System.nanoTime() + aVar.f5862b;
                aVar.f5863c.offer(cVar);
            }
        }

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

    public static final class c extends f {

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

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

    static {
        c cVar = new c(new g("RxCachedThreadSchedulerShutdown"));
        f5858f = cVar;
        cVar.dispose();
        int iMax = Math.max(1, Math.min(10, Integer.getInteger("rx2.io-priority", 5).intValue()));
        g gVar = new g("RxCachedThreadScheduler", iMax);
        f5854b = gVar;
        f5855c = new g("RxCachedWorkerPoolEvictor", iMax);
        a aVar = new a(0L, null, gVar);
        f5859g = aVar;
        aVar.f5864d.dispose();
        Future<?> future = aVar.f5866f;
        if (future != null) {
            future.cancel(true);
        }
        ScheduledExecutorService scheduledExecutorService = aVar.f5865e;
        if (scheduledExecutorService != null) {
            scheduledExecutorService.shutdownNow();
        }
    }

    public d() {
        g gVar = f5854b;
        this.f5860h = gVar;
        a aVar = f5859g;
        AtomicReference<a> atomicReference = new AtomicReference<>(aVar);
        this.f5861i = atomicReference;
        a aVar2 = new a(f5856d, f5857e, gVar);
        if (atomicReference.compareAndSet(aVar, aVar2)) {
            return;
        }
        aVar2.f5864d.dispose();
        Future<?> future = aVar2.f5866f;
        if (future != null) {
            future.cancel(true);
        }
        ScheduledExecutorService scheduledExecutorService = aVar2.f5865e;
        if (scheduledExecutorService != null) {
            scheduledExecutorService.shutdownNow();
        }
    }

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