package io.reactivex.internal.schedulers; import io.reactivex.Scheduler; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.Disposable; import io.reactivex.internal.disposables.EmptyDisposable; import java.util.Iterator; 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; /* loaded from: classes2.dex */ public final class IoScheduler extends Scheduler { static final RxThreadFactory d; static final RxThreadFactory e; private static final TimeUnit f = TimeUnit.SECONDS; static final ThreadWorker g = new ThreadWorker(new RxThreadFactory("RxCachedThreadSchedulerShutdown")); static final CachedWorkerPool h; final ThreadFactory b; final AtomicReference c; static final class EventLoopWorker extends Scheduler.Worker { private final CachedWorkerPool b; private final ThreadWorker c; final AtomicBoolean d = new AtomicBoolean(); private final CompositeDisposable a = new CompositeDisposable(); EventLoopWorker(CachedWorkerPool cachedWorkerPool) { this.b = cachedWorkerPool; this.c = cachedWorkerPool.b(); } @Override // io.reactivex.Scheduler.Worker public Disposable a(Runnable runnable, long j, TimeUnit timeUnit) { return this.a.b() ? EmptyDisposable.INSTANCE : this.c.a(runnable, j, timeUnit, this.a); } @Override // io.reactivex.disposables.Disposable public void dispose() { if (this.d.compareAndSet(false, true)) { this.a.dispose(); this.b.a(this.c); } } } static final class ThreadWorker extends NewThreadWorker { private long c; ThreadWorker(ThreadFactory threadFactory) { super(threadFactory); this.c = 0L; } public void a(long j) { this.c = j; } public long b() { return this.c; } } static { g.dispose(); int max = Math.max(1, Math.min(10, Integer.getInteger("rx2.io-priority", 5).intValue())); d = new RxThreadFactory("RxCachedThreadScheduler", max); e = new RxThreadFactory("RxCachedWorkerPoolEvictor", max); h = new CachedWorkerPool(0L, null, d); h.d(); } public IoScheduler() { this(d); } @Override // io.reactivex.Scheduler public Scheduler.Worker a() { return new EventLoopWorker(this.c.get()); } public void b() { CachedWorkerPool cachedWorkerPool = new CachedWorkerPool(60L, f, this.b); if (this.c.compareAndSet(h, cachedWorkerPool)) { return; } cachedWorkerPool.d(); } static final class CachedWorkerPool implements Runnable { private final long a; private final ConcurrentLinkedQueue b; final CompositeDisposable c; private final ScheduledExecutorService d; private final Future e; private final ThreadFactory f; CachedWorkerPool(long j, TimeUnit timeUnit, ThreadFactory threadFactory) { ScheduledFuture scheduledFuture; this.a = timeUnit != null ? timeUnit.toNanos(j) : 0L; this.b = new ConcurrentLinkedQueue<>(); this.c = new CompositeDisposable(); this.f = threadFactory; ScheduledExecutorService scheduledExecutorService = null; if (timeUnit != null) { scheduledExecutorService = Executors.newScheduledThreadPool(1, IoScheduler.e); long j2 = this.a; scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(this, j2, j2, TimeUnit.NANOSECONDS); } else { scheduledFuture = null; } this.d = scheduledExecutorService; this.e = scheduledFuture; } void a(ThreadWorker threadWorker) { threadWorker.a(c() + this.a); this.b.offer(threadWorker); } ThreadWorker b() { if (this.c.b()) { return IoScheduler.g; } while (!this.b.isEmpty()) { ThreadWorker poll = this.b.poll(); if (poll != null) { return poll; } } ThreadWorker threadWorker = new ThreadWorker(this.f); this.c.b(threadWorker); return threadWorker; } long c() { return System.nanoTime(); } void d() { this.c.dispose(); Future future = this.e; if (future != null) { future.cancel(true); } ScheduledExecutorService scheduledExecutorService = this.d; if (scheduledExecutorService != null) { scheduledExecutorService.shutdownNow(); } } @Override // java.lang.Runnable public void run() { a(); } void a() { if (this.b.isEmpty()) { return; } long c = c(); Iterator it = this.b.iterator(); while (it.hasNext()) { ThreadWorker next = it.next(); if (next.b() > c) { return; } if (this.b.remove(next)) { this.c.a(next); } } } } public IoScheduler(ThreadFactory threadFactory) { this.b = threadFactory; this.c = new AtomicReference<>(h); b(); } }