package e.a.u.g;

import e.a.n;
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 c extends n {

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

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

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

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    final ThreadFactory f3811b;

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

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

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

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private final ConcurrentLinkedQueue<C0118c> f3814f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        final e.a.r.a f3815g;

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

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

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

        a(long j2, TimeUnit timeUnit, ThreadFactory threadFactory) {
            ScheduledFuture<?> scheduledFutureScheduleWithFixedDelay;
            this.f3813b = timeUnit != null ? timeUnit.toNanos(j2) : 0L;
            this.f3814f = new ConcurrentLinkedQueue<>();
            this.f3815g = new e.a.r.a();
            this.f3818j = threadFactory;
            ScheduledExecutorService scheduledExecutorServiceNewScheduledThreadPool = null;
            if (timeUnit != null) {
                scheduledExecutorServiceNewScheduledThreadPool = Executors.newScheduledThreadPool(1, c.f3807e);
                long j3 = this.f3813b;
                scheduledFutureScheduleWithFixedDelay = scheduledExecutorServiceNewScheduledThreadPool.scheduleWithFixedDelay(this, j3, j3, TimeUnit.NANOSECONDS);
            } else {
                scheduledFutureScheduleWithFixedDelay = null;
            }
            this.f3816h = scheduledExecutorServiceNewScheduledThreadPool;
            this.f3817i = scheduledFutureScheduleWithFixedDelay;
        }

        void a() {
            if (this.f3814f.isEmpty()) {
                return;
            }
            long jC = c();
            for (C0118c c0118c : this.f3814f) {
                if (c0118c.i() > jC) {
                    return;
                }
                if (this.f3814f.remove(c0118c)) {
                    this.f3815g.b(c0118c);
                }
            }
        }

        C0118c b() {
            if (this.f3815g.isDisposed()) {
                return c.f3809g;
            }
            while (!this.f3814f.isEmpty()) {
                C0118c c0118cPoll = this.f3814f.poll();
                if (c0118cPoll != null) {
                    return c0118cPoll;
                }
            }
            C0118c c0118c = new C0118c(this.f3818j);
            this.f3815g.c(c0118c);
            return c0118c;
        }

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

        void d(C0118c c0118c) {
            c0118c.j(c() + this.f3813b);
            this.f3814f.offer(c0118c);
        }

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

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

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

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

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

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

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final e.a.r.a f3819b = new e.a.r.a();

        b(a aVar) {
            this.f3820f = aVar;
            this.f3821g = aVar.b();
        }

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

        @Override // e.a.r.b
        public void dispose() {
            if (this.f3822h.compareAndSet(false, true)) {
                this.f3819b.dispose();
                this.f3820f.d(this.f3821g);
            }
        }

        @Override // e.a.r.b
        public boolean isDisposed() {
            return this.f3822h.get();
        }
    }

    /* JADX INFO: renamed from: e.a.u.g.c$c, reason: collision with other inner class name */
    /* JADX INFO: compiled from: IoScheduler.java */
    static final class C0118c extends e {

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

        C0118c(ThreadFactory threadFactory) {
            super(threadFactory);
            this.f3823g = 0L;
        }

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

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

    static {
        C0118c c0118c = new C0118c(new f("RxCachedThreadSchedulerShutdown"));
        f3809g = c0118c;
        c0118c.dispose();
        int iMax = Math.max(1, Math.min(10, Integer.getInteger("rx2.io-priority", 5).intValue()));
        f3806d = new f("RxCachedThreadScheduler", iMax);
        f3807e = new f("RxCachedWorkerPoolEvictor", iMax);
        a aVar = new a(0L, null, f3806d);
        f3810h = aVar;
        aVar.e();
    }

    public c() {
        this(f3806d);
    }

    @Override // e.a.n
    public n.c a() {
        return new b(this.f3812c.get());
    }

    public void e() {
        a aVar = new a(60L, f3808f, this.f3811b);
        if (this.f3812c.compareAndSet(f3810h, aVar)) {
            return;
        }
        aVar.e();
    }

    public c(ThreadFactory threadFactory) {
        this.f3811b = threadFactory;
        this.f3812c = new AtomicReference<>(f3810h);
        e();
    }
}
