what-the-bank/sources/com/google/common/util/concurrent/JdkFutureAdapters.java

90 lines
3.5 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.util.concurrent;
import com.google.common.base.Preconditions;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;
/* loaded from: classes2.dex */
public final class JdkFutureAdapters {
public static <V> ListenableFuture<V> listenInPoolThread(Future<V> future) {
if (future instanceof ListenableFuture) {
return (ListenableFuture) future;
}
return new ListenableFutureAdapter(future);
}
public static <V> ListenableFuture<V> listenInPoolThread(Future<V> future, Executor executor) {
Preconditions.checkNotNull(executor);
if (future instanceof ListenableFuture) {
return (ListenableFuture) future;
}
return new ListenableFutureAdapter(future, executor);
}
/* loaded from: classes2.dex */
static class ListenableFutureAdapter<V> extends ForwardingFuture<V> implements ListenableFuture<V> {
private static final Executor defaultAdapterExecutor;
private static final ThreadFactory threadFactory;
private final Executor adapterExecutor;
private final Future<V> delegate;
private final ExecutionList executionList;
private final AtomicBoolean hasListeners;
static {
ThreadFactory build = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("ListenableFutureAdapter-thread-%d").build();
threadFactory = build;
defaultAdapterExecutor = Executors.newCachedThreadPool(build);
}
ListenableFutureAdapter(Future<V> future) {
this(future, defaultAdapterExecutor);
}
ListenableFutureAdapter(Future<V> future, Executor executor) {
this.executionList = new ExecutionList();
this.hasListeners = new AtomicBoolean(false);
this.delegate = (Future) Preconditions.checkNotNull(future);
this.adapterExecutor = (Executor) Preconditions.checkNotNull(executor);
}
@Override // com.google.common.util.concurrent.ListenableFuture
public void addListener(Runnable runnable, Executor executor) {
this.executionList.add(runnable, executor);
if (this.hasListeners.compareAndSet(false, true)) {
if (this.delegate.isDone()) {
this.executionList.execute();
} else {
this.adapterExecutor.execute(new Runnable(this) { // from class: com.google.common.util.concurrent.JdkFutureAdapters.ListenableFutureAdapter.1
final ListenableFutureAdapter this$0;
{
this.this$0 = this;
}
@Override // java.lang.Runnable
public void run() {
try {
Uninterruptibles.getUninterruptibly(this.this$0.delegate);
} catch (Throwable unused) {
}
this.this$0.executionList.execute();
}
});
}
}
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.util.concurrent.ForwardingFuture, com.google.common.collect.ForwardingObject
public Future<V> delegate() {
return this.delegate;
}
}
private JdkFutureAdapters() {
}
}