package com.google.firebase.concurrent; import com.google.firebase.components.Preconditions; import java.util.concurrent.Executor; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.Semaphore; /* loaded from: classes3.dex */ public class LimitedConcurrencyExecutor implements Executor { private final Executor delegate; private final LinkedBlockingQueue queue = new LinkedBlockingQueue<>(); private final Semaphore semaphore; public LimitedConcurrencyExecutor(Executor executor, int i) { Preconditions.checkArgument(i > 0, "concurrency must be positive."); this.delegate = executor; this.semaphore = new Semaphore(i, true); } public static /* synthetic */ void a(LimitedConcurrencyExecutor limitedConcurrencyExecutor, Runnable runnable) { limitedConcurrencyExecutor.lambda$decorate$0(runnable); } private Runnable decorate(Runnable runnable) { return new a(2, this, runnable); } public /* synthetic */ void lambda$decorate$0(Runnable runnable) { try { runnable.run(); } finally { this.semaphore.release(); maybeEnqueueNext(); } } private void maybeEnqueueNext() { while (this.semaphore.tryAcquire()) { Runnable poll = this.queue.poll(); if (poll == null) { this.semaphore.release(); return; } this.delegate.execute(decorate(poll)); } } @Override // java.util.concurrent.Executor public void execute(Runnable runnable) { this.queue.offer(runnable); maybeEnqueueNext(); } }