package c.a.q.g;

import c.a.g;
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: compiled from: IoScheduler.java */
/* JADX INFO: loaded from: classes.dex */
public final class d extends c.a.g {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    static final h f1844b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static final TimeUnit f1846d = TimeUnit.SECONDS;

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    final ThreadFactory f1849g;
    final AtomicReference<a> h;

    /* JADX INFO: compiled from: IoScheduler.java */
    static final class a implements Runnable {

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

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

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        final c.a.n.a f1852e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private final ScheduledExecutorService f1853f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private final Future<?> f1854g;
        private final ThreadFactory h;

        a(long j, TimeUnit timeUnit, ThreadFactory threadFactory) {
            ScheduledFuture<?> scheduledFutureScheduleWithFixedDelay;
            long nanos = timeUnit != null ? timeUnit.toNanos(j) : 0L;
            this.f1850c = nanos;
            this.f1851d = new ConcurrentLinkedQueue<>();
            this.f1852e = new c.a.n.a();
            this.h = threadFactory;
            ScheduledExecutorService scheduledExecutorServiceNewScheduledThreadPool = null;
            if (timeUnit != null) {
                scheduledExecutorServiceNewScheduledThreadPool = Executors.newScheduledThreadPool(1, d.f1845c);
                scheduledFutureScheduleWithFixedDelay = scheduledExecutorServiceNewScheduledThreadPool.scheduleWithFixedDelay(this, nanos, nanos, TimeUnit.NANOSECONDS);
            } else {
                scheduledFutureScheduleWithFixedDelay = null;
            }
            this.f1853f = scheduledExecutorServiceNewScheduledThreadPool;
            this.f1854g = scheduledFutureScheduleWithFixedDelay;
        }

        void a() {
            if (this.f1851d.isEmpty()) {
                return;
            }
            long jC = c();
            for (c cVar : this.f1851d) {
                if (cVar.i() > jC) {
                    return;
                }
                if (this.f1851d.remove(cVar)) {
                    this.f1852e.a(cVar);
                }
            }
        }

        c b() {
            if (this.f1852e.f()) {
                return d.f1847e;
            }
            while (!this.f1851d.isEmpty()) {
                c cVarPoll = this.f1851d.poll();
                if (cVarPoll != null) {
                    return cVarPoll;
                }
            }
            c cVar = new c(this.h);
            this.f1852e.b(cVar);
            return cVar;
        }

        long c() {
            return System.nanoTime();
        }

        void d(c cVar) {
            cVar.j(c() + this.f1850c);
            this.f1851d.offer(cVar);
        }

        void e() {
            this.f1852e.dispose();
            Future<?> future = this.f1854g;
            if (future != null) {
                future.cancel(true);
            }
            ScheduledExecutorService scheduledExecutorService = this.f1853f;
            if (scheduledExecutorService != null) {
                scheduledExecutorService.shutdownNow();
            }
        }

        @Override // java.lang.Runnable
        public void run() {
            a();
        }
    }

    /* JADX INFO: compiled from: IoScheduler.java */
    static final class b extends g.c {

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private final a f1856d;

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

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

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private final c.a.n.a f1855c = new c.a.n.a();

        b(a aVar) {
            this.f1856d = aVar;
            this.f1857e = aVar.b();
        }

        @Override // c.a.g.c
        public c.a.n.b c(Runnable runnable, long j, TimeUnit timeUnit) {
            return this.f1855c.f() ? c.a.q.a.c.INSTANCE : this.f1857e.e(runnable, j, timeUnit, this.f1855c);
        }

        @Override // c.a.n.b
        public void dispose() {
            if (this.f1858f.compareAndSet(false, true)) {
                this.f1855c.dispose();
                this.f1856d.d(this.f1857e);
            }
        }
    }

    /* JADX INFO: compiled from: IoScheduler.java */
    static final class c extends f {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private long f1859e;

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

        public long i() {
            return this.f1859e;
        }

        public void j(long j) {
            this.f1859e = j;
        }
    }

    static {
        c cVar = new c(new h("RxCachedThreadSchedulerShutdown"));
        f1847e = cVar;
        cVar.dispose();
        int iMax = Math.max(1, Math.min(10, Integer.getInteger("rx2.io-priority", 5).intValue()));
        h hVar = new h("RxCachedThreadScheduler", iMax);
        f1844b = hVar;
        f1845c = new h("RxCachedWorkerPoolEvictor", iMax);
        a aVar = new a(0L, null, hVar);
        f1848f = aVar;
        aVar.e();
    }

    public d() {
        this(f1844b);
    }

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

    public void e() {
        a aVar = new a(60L, f1846d, this.f1849g);
        if (this.h.compareAndSet(f1848f, aVar)) {
            return;
        }
        aVar.e();
    }

    public d(ThreadFactory threadFactory) {
        this.f1849g = threadFactory;
        this.h = new AtomicReference<>(f1848f);
        e();
    }
}
