Files
rr3-apk/decompiled/sources/com/mbridge/msdk/thrid/okhttp/Dispatcher.java
Daniel Elliott f9d20bb3fc Add decompiled APK source code (JADX)
- 28,932 files
- Full Java source code
- Smali files
- Resources

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-02-18 14:52:23 -08:00

206 lines
6.3 KiB
Java

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<RealCall.AsyncCall> readyAsyncCalls = new ArrayDeque();
private final Deque<RealCall.AsyncCall> runningAsyncCalls = new ArrayDeque();
private final Deque<RealCall> 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<RealCall.AsyncCall> it = this.readyAsyncCalls.iterator();
while (it.hasNext()) {
it.next().get().cancel();
}
Iterator<RealCall.AsyncCall> it2 = this.runningAsyncCalls.iterator();
while (it2.hasNext()) {
it2.next().get().cancel();
}
Iterator<RealCall> 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<RealCall.AsyncCall> 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 <T> void finished(Deque<T> 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<Call> queuedCalls() {
ArrayList arrayList;
try {
arrayList = new ArrayList();
Iterator<RealCall.AsyncCall> it = this.readyAsyncCalls.iterator();
while (it.hasNext()) {
arrayList.add(it.next().get());
}
} catch (Throwable th) {
throw th;
}
return Collections.unmodifiableList(arrayList);
}
public synchronized List<Call> runningCalls() {
ArrayList arrayList;
try {
arrayList = new ArrayList();
arrayList.addAll(this.runningSyncCalls);
Iterator<RealCall.AsyncCall> 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();
}
}