package f.a.n.g;

import f.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 f.a.g {

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

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

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

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

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

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

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

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

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        final f.a.k.a f895f;

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

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

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

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

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

        c b() {
            if (this.f895f.f()) {
                return d.f891g;
            }
            while (!this.f894e.isEmpty()) {
                c cVarPoll = this.f894e.poll();
                if (cVarPoll != null) {
                    return cVarPoll;
                }
            }
            c cVar = new c(this.f898i);
            this.f895f.c(cVar);
            return cVar;
        }

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

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

        void e() {
            this.f895f.dispose();
            Future<?> future = this.f897h;
            if (future != null) {
                future.cancel(true);
            }
            ScheduledExecutorService scheduledExecutorService = this.f896g;
            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: e, reason: collision with root package name */
        private final a f899e;

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

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        final AtomicBoolean f901g = new AtomicBoolean();
        private final f.a.k.a b = new f.a.k.a();

        b(a aVar) {
            this.f899e = aVar;
            this.f900f = aVar.b();
        }

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

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

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

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

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

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

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

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

    public d() {
        this(f888d);
    }

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

    public void e() {
        a aVar = new a(60L, f890f, this.b);
        if (this.f893c.compareAndSet(f892h, aVar)) {
            return;
        }
        aVar.e();
    }

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