143 lines
5.1 KiB
Java
143 lines
5.1 KiB
Java
package androidx.core.provider;
|
|
|
|
import android.os.Handler;
|
|
import android.os.Process;
|
|
import androidx.annotation.IntRange;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.core.util.Consumer;
|
|
import androidx.core.util.Preconditions;
|
|
import java.util.concurrent.Callable;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.Executor;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.LinkedBlockingDeque;
|
|
import java.util.concurrent.RejectedExecutionException;
|
|
import java.util.concurrent.ThreadFactory;
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeoutException;
|
|
/* loaded from: classes.dex */
|
|
public class RequestExecutor {
|
|
|
|
/* loaded from: classes.dex */
|
|
public static class DefaultThreadFactory implements ThreadFactory {
|
|
private int mPriority;
|
|
private String mThreadName;
|
|
|
|
/* loaded from: classes.dex */
|
|
public static class ProcessPriorityThread extends Thread {
|
|
private final int mPriority;
|
|
|
|
public ProcessPriorityThread(Runnable runnable, String str, int i) {
|
|
super(runnable, str);
|
|
this.mPriority = i;
|
|
}
|
|
|
|
@Override // java.lang.Thread, java.lang.Runnable
|
|
public void run() {
|
|
Process.setThreadPriority(this.mPriority);
|
|
super.run();
|
|
}
|
|
}
|
|
|
|
public DefaultThreadFactory(@NonNull String str, int i) {
|
|
this.mThreadName = str;
|
|
this.mPriority = i;
|
|
}
|
|
|
|
@Override // java.util.concurrent.ThreadFactory
|
|
public Thread newThread(Runnable runnable) {
|
|
return new ProcessPriorityThread(runnable, this.mThreadName, this.mPriority);
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes.dex */
|
|
public static class HandlerExecutor implements Executor {
|
|
private final Handler mHandler;
|
|
|
|
public HandlerExecutor(@NonNull Handler handler) {
|
|
this.mHandler = (Handler) Preconditions.checkNotNull(handler);
|
|
}
|
|
|
|
@Override // java.util.concurrent.Executor
|
|
public void execute(@NonNull Runnable runnable) {
|
|
if (!this.mHandler.post((Runnable) Preconditions.checkNotNull(runnable))) {
|
|
throw new RejectedExecutionException(this.mHandler + " is shutting down");
|
|
}
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes.dex */
|
|
public static class ReplyRunnable<T> implements Runnable {
|
|
@NonNull
|
|
private Callable<T> mCallable;
|
|
@NonNull
|
|
private Consumer<T> mConsumer;
|
|
@NonNull
|
|
private Handler mHandler;
|
|
|
|
/* renamed from: androidx.core.provider.RequestExecutor$ReplyRunnable$1 reason: invalid class name */
|
|
/* loaded from: classes.dex */
|
|
public class AnonymousClass1 implements Runnable {
|
|
public final /* synthetic */ Consumer val$consumer;
|
|
public final /* synthetic */ Object val$result;
|
|
|
|
public AnonymousClass1(Consumer consumer, Object obj) {
|
|
this.val$consumer = consumer;
|
|
this.val$result = obj;
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
this.val$consumer.accept(this.val$result);
|
|
}
|
|
}
|
|
|
|
public ReplyRunnable(@NonNull Handler handler, @NonNull Callable<T> callable, @NonNull Consumer<T> consumer) {
|
|
this.mCallable = callable;
|
|
this.mConsumer = consumer;
|
|
this.mHandler = handler;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
T t;
|
|
try {
|
|
t = this.mCallable.call();
|
|
} catch (Exception unused) {
|
|
t = null;
|
|
}
|
|
this.mHandler.post(new AnonymousClass1(this.mConsumer, t));
|
|
}
|
|
}
|
|
|
|
private RequestExecutor() {
|
|
}
|
|
|
|
public static ThreadPoolExecutor createDefaultExecutor(@NonNull String str, int i, @IntRange(from = 0) int i2) {
|
|
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, 1, i2, TimeUnit.MILLISECONDS, new LinkedBlockingDeque(), new DefaultThreadFactory(str, i));
|
|
threadPoolExecutor.allowCoreThreadTimeOut(true);
|
|
return threadPoolExecutor;
|
|
}
|
|
|
|
public static Executor createHandlerExecutor(@NonNull Handler handler) {
|
|
return new HandlerExecutor(handler);
|
|
}
|
|
|
|
public static <T> void execute(@NonNull Executor executor, @NonNull Callable<T> callable, @NonNull Consumer<T> consumer) {
|
|
executor.execute(new ReplyRunnable(CalleeHandler.create(), callable, consumer));
|
|
}
|
|
|
|
public static <T> T submit(@NonNull ExecutorService executorService, @NonNull Callable<T> callable, @IntRange(from = 0) int i) throws InterruptedException {
|
|
try {
|
|
return executorService.submit(callable).get(i, TimeUnit.MILLISECONDS);
|
|
} catch (InterruptedException e) {
|
|
throw e;
|
|
} catch (ExecutionException e2) {
|
|
throw new RuntimeException(e2);
|
|
} catch (TimeoutException unused) {
|
|
throw new InterruptedException("timeout");
|
|
}
|
|
}
|
|
}
|