package i.a.n.g;

import i.a.h;
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 i.a.h {
    static final g d;
    static final g e;
    private static final TimeUnit f = TimeUnit.SECONDS;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    static final a f872h;
    final ThreadFactory b;
    final AtomicReference<a> c;

    /* JADX INFO: compiled from: IoScheduler.java */
    static final class a implements Runnable {
        private final long b;
        private final ConcurrentLinkedQueue<c> d;
        final i.a.k.a e;
        private final ScheduledExecutorService f;

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

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

        a(long j2, TimeUnit timeUnit, ThreadFactory threadFactory) {
            ScheduledFuture<?> scheduledFutureScheduleWithFixedDelay;
            this.b = timeUnit != null ? timeUnit.toNanos(j2) : 0L;
            this.d = new ConcurrentLinkedQueue<>();
            this.e = new i.a.k.a();
            this.f874h = threadFactory;
            ScheduledExecutorService scheduledExecutorServiceNewScheduledThreadPool = null;
            if (timeUnit != null) {
                scheduledExecutorServiceNewScheduledThreadPool = Executors.newScheduledThreadPool(1, d.e);
                long j3 = this.b;
                scheduledFutureScheduleWithFixedDelay = scheduledExecutorServiceNewScheduledThreadPool.scheduleWithFixedDelay(this, j3, j3, TimeUnit.NANOSECONDS);
            } else {
                scheduledFutureScheduleWithFixedDelay = null;
            }
            this.f = scheduledExecutorServiceNewScheduledThreadPool;
            this.f873g = scheduledFutureScheduleWithFixedDelay;
        }

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

        c b() {
            if (this.e.isDisposed()) {
                return d.f871g;
            }
            while (!this.d.isEmpty()) {
                c cVarPoll = this.d.poll();
                if (cVarPoll != null) {
                    return cVarPoll;
                }
            }
            c cVar = new c(this.f874h);
            this.e.c(cVar);
            return cVar;
        }

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

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

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

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

    /* JADX INFO: compiled from: IoScheduler.java */
    static final class b extends h.c {
        private final a d;
        private final c e;
        final AtomicBoolean f = new AtomicBoolean();
        private final i.a.k.a b = new i.a.k.a();

        b(a aVar) {
            this.d = aVar;
            this.e = aVar.b();
        }

        @Override // i.a.h.c
        public i.a.k.b c(Runnable runnable, long j2, TimeUnit timeUnit) {
            return this.b.isDisposed() ? i.a.n.a.c.INSTANCE : this.e.e(runnable, j2, timeUnit, this.b);
        }

        @Override // i.a.k.b
        public void dispose() {
            if (this.f.compareAndSet(false, true)) {
                this.b.dispose();
                this.d.d(this.e);
            }
        }

        @Override // i.a.k.b
        public boolean isDisposed() {
            return this.f.get();
        }
    }

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

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

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

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

    static {
        c cVar = new c(new g("RxCachedThreadSchedulerShutdown"));
        f871g = cVar;
        cVar.dispose();
        int iMax = Math.max(1, Math.min(10, Integer.getInteger("rx2.io-priority", 5).intValue()));
        d = new g("RxCachedThreadScheduler", iMax);
        e = new g("RxCachedWorkerPoolEvictor", iMax);
        a aVar = new a(0L, null, d);
        f872h = aVar;
        aVar.e();
    }

    public d() {
        this(d);
    }

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

    public void e() {
        a aVar = new a(60L, f, this.b);
        if (this.c.compareAndSet(f872h, aVar)) {
            return;
        }
        aVar.e();
    }

    public d(ThreadFactory threadFactory) {
        this.b = threadFactory;
        this.c = new AtomicReference<>(f872h);
        e();
    }
}
