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 ListenableFuture listenInPoolThread(Future future) { if (future instanceof ListenableFuture) { return (ListenableFuture) future; } return new ListenableFutureAdapter(future); } public static ListenableFuture listenInPoolThread(Future 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 extends ForwardingFuture implements ListenableFuture { private static final Executor defaultAdapterExecutor; private static final ThreadFactory threadFactory; private final Executor adapterExecutor; private final Future 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 future) { this(future, defaultAdapterExecutor); } ListenableFutureAdapter(Future 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 delegate() { return this.delegate; } } private JdkFutureAdapters() { } }