package com.mbridge.msdk.thrid.okhttp; import com.mbridge.msdk.thrid.okhttp.RealCall; import com.mbridge.msdk.thrid.okhttp.internal.Util; 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; /* loaded from: classes4.dex */ public final class Dispatcher { static final /* synthetic */ boolean $assertionsDisabled = false; private ExecutorService executorService; private Runnable idleCallback; private int maxRequests = 64; private int maxRequestsPerHost = 5; private final Deque readyAsyncCalls = new ArrayDeque(); private final Deque runningAsyncCalls = new ArrayDeque(); private final Deque runningSyncCalls = new ArrayDeque(); public Dispatcher(ExecutorService executorService) { this.executorService = executorService; } public Dispatcher() { } public synchronized ExecutorService executorService() { try { if (this.executorService == null) { this.executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory("OkHttp Dispatcher", false)); } } catch (Throwable th) { throw th; } return this.executorService; } public void setMaxRequests(int i) { if (i < 1) { throw new IllegalArgumentException("max < 1: " + i); } synchronized (this) { this.maxRequests = i; } promoteAndExecute(); } public synchronized int getMaxRequests() { return this.maxRequests; } public void setMaxRequestsPerHost(int i) { if (i < 1) { throw new IllegalArgumentException("max < 1: " + i); } synchronized (this) { this.maxRequestsPerHost = i; } promoteAndExecute(); } public synchronized int getMaxRequestsPerHost() { return this.maxRequestsPerHost; } public synchronized void setIdleCallback(Runnable runnable) { this.idleCallback = runnable; } public void enqueue(RealCall.AsyncCall asyncCall) { synchronized (this) { this.readyAsyncCalls.add(asyncCall); } promoteAndExecute(); } public synchronized void cancelAll() { try { Iterator it = this.readyAsyncCalls.iterator(); while (it.hasNext()) { it.next().get().cancel(); } Iterator it2 = this.runningAsyncCalls.iterator(); while (it2.hasNext()) { it2.next().get().cancel(); } Iterator it3 = this.runningSyncCalls.iterator(); while (it3.hasNext()) { it3.next().cancel(); } } catch (Throwable th) { throw th; } } private boolean promoteAndExecute() { int i; boolean z; ArrayList arrayList = new ArrayList(); synchronized (this) { try { Iterator it = this.readyAsyncCalls.iterator(); while (it.hasNext()) { RealCall.AsyncCall next = it.next(); if (this.runningAsyncCalls.size() >= this.maxRequests) { break; } if (runningCallsForHost(next) < this.maxRequestsPerHost) { it.remove(); arrayList.add(next); this.runningAsyncCalls.add(next); } } z = runningCallsCount() > 0; } catch (Throwable th) { throw th; } } int size = arrayList.size(); for (i = 0; i < size; i++) { ((RealCall.AsyncCall) arrayList.get(i)).executeOn(executorService()); } return z; } private int runningCallsForHost(RealCall.AsyncCall asyncCall) { int i = 0; for (RealCall.AsyncCall asyncCall2 : this.runningAsyncCalls) { if (!asyncCall2.get().forWebSocket && asyncCall2.host().equals(asyncCall.host())) { i++; } } return i; } public synchronized void executed(RealCall realCall) { this.runningSyncCalls.add(realCall); } public void finished(RealCall.AsyncCall asyncCall) { finished(this.runningAsyncCalls, asyncCall); } public void finished(RealCall realCall) { finished(this.runningSyncCalls, realCall); } private void finished(Deque deque, T t) { Runnable runnable; synchronized (this) { if (!deque.remove(t)) { throw new AssertionError("Call wasn't in-flight!"); } runnable = this.idleCallback; } if (promoteAndExecute() || runnable == null) { return; } runnable.run(); } public synchronized List queuedCalls() { ArrayList arrayList; try { arrayList = new ArrayList(); Iterator it = this.readyAsyncCalls.iterator(); while (it.hasNext()) { arrayList.add(it.next().get()); } } catch (Throwable th) { throw th; } return Collections.unmodifiableList(arrayList); } public synchronized List runningCalls() { ArrayList arrayList; try { arrayList = new ArrayList(); arrayList.addAll(this.runningSyncCalls); Iterator it = this.runningAsyncCalls.iterator(); while (it.hasNext()) { arrayList.add(it.next().get()); } } catch (Throwable th) { throw th; } return Collections.unmodifiableList(arrayList); } public synchronized int queuedCallsCount() { return this.readyAsyncCalls.size(); } public synchronized int runningCallsCount() { return this.runningAsyncCalls.size() + this.runningSyncCalls.size(); } }