package okhttp3.repackaged; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; import java.util.Deque; import java.util.Iterator; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import okhttp3.repackaged.a; import okhttp3.repackaged.internal.Util; /* loaded from: classes6.dex */ public final class Dispatcher { private ExecutorService ahx; private int ahv = 64; private int ahw = 5; private final Deque ahy = new ArrayDeque(); private final Deque ahz = new ArrayDeque(); private final Deque ahA = new ArrayDeque(); public Dispatcher(ExecutorService executorService) { this.ahx = executorService; } public Dispatcher() { } public final ExecutorService executorService() { ExecutorService executorService; synchronized (this) { if (this.ahx == null) { this.ahx = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp Dispatcher", false)); } executorService = this.ahx; } return executorService; } public final void setMaxRequests(int i) { synchronized (this) { if (i <= 0) { StringBuilder sb = new StringBuilder("max < 1: "); sb.append(i); throw new IllegalArgumentException(sb.toString()); } this.ahv = i; un(); } } public final int getMaxRequests() { int i; synchronized (this) { i = this.ahv; } return i; } public final void setMaxRequestsPerHost(int i) { synchronized (this) { if (i <= 0) { StringBuilder sb = new StringBuilder("max < 1: "); sb.append(i); throw new IllegalArgumentException(sb.toString()); } this.ahw = i; un(); } } public final int getMaxRequestsPerHost() { int i; synchronized (this) { i = this.ahw; } return i; } /* JADX INFO: Access modifiers changed from: package-private */ public final void a(a.b bVar) { synchronized (this) { if (this.ahz.size() < this.ahv && c(bVar) < this.ahw) { this.ahz.add(bVar); executorService().execute(bVar); } else { this.ahy.add(bVar); } } } public final void cancelAll() { synchronized (this) { Iterator it = this.ahy.iterator(); while (it.hasNext()) { it.next().cancel(); } Iterator it2 = this.ahz.iterator(); while (it2.hasNext()) { it2.next().cancel(); } Iterator it3 = this.ahA.iterator(); while (it3.hasNext()) { it3.next().cancel(); } } } /* JADX INFO: Access modifiers changed from: package-private */ public final void b(a.b bVar) { synchronized (this) { if (!this.ahz.remove(bVar)) { throw new AssertionError("AsyncCall wasn't running!"); } un(); } } private void un() { if (this.ahz.size() < this.ahv && !this.ahy.isEmpty()) { Iterator it = this.ahy.iterator(); while (it.hasNext()) { a.b next = it.next(); if (c(next) < this.ahw) { it.remove(); this.ahz.add(next); executorService().execute(next); } if (this.ahz.size() >= this.ahv) { return; } } } } private int c(a.b bVar) { Iterator it = this.ahz.iterator(); int i = 0; while (it.hasNext()) { if (it.next().host().equals(bVar.host())) { i++; } } return i; } /* JADX INFO: Access modifiers changed from: package-private */ public final void a(a aVar) { synchronized (this) { this.ahA.add(aVar); } } /* JADX INFO: Access modifiers changed from: package-private */ public final void a(Call call) { synchronized (this) { if (!this.ahA.remove(call)) { throw new AssertionError("Call wasn't in-flight!"); } } } public final List queuedCalls() { List unmodifiableList; synchronized (this) { ArrayList arrayList = new ArrayList(); Iterator it = this.ahy.iterator(); while (it.hasNext()) { arrayList.add(it.next().us()); } unmodifiableList = Collections.unmodifiableList(arrayList); } return unmodifiableList; } public final List runningCalls() { List unmodifiableList; synchronized (this) { ArrayList arrayList = new ArrayList(); arrayList.addAll(this.ahA); Iterator it = this.ahz.iterator(); while (it.hasNext()) { arrayList.add(it.next().us()); } unmodifiableList = Collections.unmodifiableList(arrayList); } return unmodifiableList; } public final int queuedCallsCount() { int size; synchronized (this) { size = this.ahy.size(); } return size; } public final int runningCallsCount() { int size; int size2; synchronized (this) { size = this.ahz.size(); size2 = this.ahA.size(); } return size + size2; } }