package com.google.common.util.concurrent; import com.google.common.base.Preconditions; import com.google.common.base.Supplier; import com.google.common.base.Throwables; import com.google.common.util.concurrent.AbstractFuture; import com.google.common.util.concurrent.ForwardingListenableFuture; import java.lang.reflect.InvocationTargetException; import java.util.Collections; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.Delayed; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /* loaded from: classes2.dex */ public final class MoreExecutors { private MoreExecutors() { } public static ExecutorService getExitingExecutorService(ThreadPoolExecutor threadPoolExecutor, long j, TimeUnit timeUnit) { return new Application().getExitingExecutorService(threadPoolExecutor, j, timeUnit); } public static ExecutorService getExitingExecutorService(ThreadPoolExecutor threadPoolExecutor) { return new Application().getExitingExecutorService(threadPoolExecutor); } public static ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor scheduledThreadPoolExecutor, long j, TimeUnit timeUnit) { return new Application().getExitingScheduledExecutorService(scheduledThreadPoolExecutor, j, timeUnit); } public static ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor scheduledThreadPoolExecutor) { return new Application().getExitingScheduledExecutorService(scheduledThreadPoolExecutor); } public static void addDelayedShutdownHook(ExecutorService executorService, long j, TimeUnit timeUnit) { new Application().addDelayedShutdownHook(executorService, j, timeUnit); } /* loaded from: classes2.dex */ static class Application { Application() { } final ExecutorService getExitingExecutorService(ThreadPoolExecutor threadPoolExecutor, long j, TimeUnit timeUnit) { MoreExecutors.useDaemonThreadFactory(threadPoolExecutor); ExecutorService unconfigurableExecutorService = Executors.unconfigurableExecutorService(threadPoolExecutor); addDelayedShutdownHook(threadPoolExecutor, j, timeUnit); return unconfigurableExecutorService; } final ExecutorService getExitingExecutorService(ThreadPoolExecutor threadPoolExecutor) { return getExitingExecutorService(threadPoolExecutor, 120L, TimeUnit.SECONDS); } final ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor scheduledThreadPoolExecutor, long j, TimeUnit timeUnit) { MoreExecutors.useDaemonThreadFactory(scheduledThreadPoolExecutor); ScheduledExecutorService unconfigurableScheduledExecutorService = Executors.unconfigurableScheduledExecutorService(scheduledThreadPoolExecutor); addDelayedShutdownHook(scheduledThreadPoolExecutor, j, timeUnit); return unconfigurableScheduledExecutorService; } final ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor scheduledThreadPoolExecutor) { return getExitingScheduledExecutorService(scheduledThreadPoolExecutor, 120L, TimeUnit.SECONDS); } final void addDelayedShutdownHook(ExecutorService executorService, long j, TimeUnit timeUnit) { Preconditions.checkNotNull(executorService); Preconditions.checkNotNull(timeUnit); String valueOf = String.valueOf(executorService); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 24); sb.append("DelayedShutdownHook-for-"); sb.append(valueOf); addShutdownHook(MoreExecutors.newThread(sb.toString(), new Runnable(this, executorService, j, timeUnit) { // from class: com.google.common.util.concurrent.MoreExecutors.Application.1 final ExecutorService val$service; final long val$terminationTimeout; final TimeUnit val$timeUnit; { this.val$service = executorService; this.val$terminationTimeout = j; this.val$timeUnit = timeUnit; } @Override // java.lang.Runnable public void run() { try { this.val$service.shutdown(); this.val$service.awaitTermination(this.val$terminationTimeout, this.val$timeUnit); } catch (InterruptedException unused) { } } })); } void addShutdownHook(Thread thread) { Runtime.getRuntime().addShutdownHook(thread); } } /* JADX INFO: Access modifiers changed from: private */ public static void useDaemonThreadFactory(ThreadPoolExecutor threadPoolExecutor) { threadPoolExecutor.setThreadFactory(new ThreadFactoryBuilder().setDaemon(true).setThreadFactory(threadPoolExecutor.getThreadFactory()).build()); } /* loaded from: classes2.dex */ static final class DirectExecutorService extends AbstractListeningExecutorService { private final Object lock; private int runningTasks; private boolean shutdown; private DirectExecutorService() { this.lock = new Object(); this.runningTasks = 0; this.shutdown = false; } @Override // java.util.concurrent.Executor public final void execute(Runnable runnable) { startTask(); try { runnable.run(); } finally { endTask(); } } @Override // java.util.concurrent.ExecutorService public final boolean isShutdown() { boolean z; synchronized (this.lock) { z = this.shutdown; } return z; } @Override // java.util.concurrent.ExecutorService public final void shutdown() { synchronized (this.lock) { this.shutdown = true; if (this.runningTasks == 0) { this.lock.notifyAll(); } } } @Override // java.util.concurrent.ExecutorService public final List shutdownNow() { shutdown(); return Collections.emptyList(); } @Override // java.util.concurrent.ExecutorService public final boolean isTerminated() { boolean z; synchronized (this.lock) { if (this.shutdown) { z = this.runningTasks == 0; } } return z; } @Override // java.util.concurrent.ExecutorService public final boolean awaitTermination(long j, TimeUnit timeUnit) throws InterruptedException { long nanos = timeUnit.toNanos(j); synchronized (this.lock) { while (true) { if (this.shutdown && this.runningTasks == 0) { return true; } if (nanos <= 0) { return false; } long nanoTime = System.nanoTime(); TimeUnit.NANOSECONDS.timedWait(this.lock, nanos); nanos -= System.nanoTime() - nanoTime; } } } private void startTask() { synchronized (this.lock) { if (this.shutdown) { throw new RejectedExecutionException("Executor already shutdown"); } this.runningTasks++; } } private void endTask() { synchronized (this.lock) { int i = this.runningTasks - 1; this.runningTasks = i; if (i == 0) { this.lock.notifyAll(); } } } } public static ListeningExecutorService newDirectExecutorService() { return new DirectExecutorService(); } public static Executor directExecutor() { return DirectExecutor.INSTANCE; } public static Executor newSequentialExecutor(Executor executor) { return new SequentialExecutor(executor); } public static ListeningExecutorService listeningDecorator(ExecutorService executorService) { ListeningExecutorService listeningDecorator; if (executorService instanceof ListeningExecutorService) { return (ListeningExecutorService) executorService; } if (executorService instanceof ScheduledExecutorService) { listeningDecorator = new ScheduledListeningDecorator((ScheduledExecutorService) executorService); } else { listeningDecorator = new ListeningDecorator(executorService); } return listeningDecorator; } public static ListeningScheduledExecutorService listeningDecorator(ScheduledExecutorService scheduledExecutorService) { if (scheduledExecutorService instanceof ListeningScheduledExecutorService) { return (ListeningScheduledExecutorService) scheduledExecutorService; } return new ScheduledListeningDecorator(scheduledExecutorService); } /* loaded from: classes2.dex */ static class ListeningDecorator extends AbstractListeningExecutorService { private final ExecutorService delegate; ListeningDecorator(ExecutorService executorService) { this.delegate = (ExecutorService) Preconditions.checkNotNull(executorService); } @Override // java.util.concurrent.ExecutorService public final boolean awaitTermination(long j, TimeUnit timeUnit) throws InterruptedException { return this.delegate.awaitTermination(j, timeUnit); } @Override // java.util.concurrent.ExecutorService public final boolean isShutdown() { return this.delegate.isShutdown(); } @Override // java.util.concurrent.ExecutorService public final boolean isTerminated() { return this.delegate.isTerminated(); } @Override // java.util.concurrent.ExecutorService public final void shutdown() { this.delegate.shutdown(); } @Override // java.util.concurrent.ExecutorService public final List shutdownNow() { return this.delegate.shutdownNow(); } @Override // java.util.concurrent.Executor public final void execute(Runnable runnable) { this.delegate.execute(runnable); } } /* loaded from: classes2.dex */ static final class ScheduledListeningDecorator extends ListeningDecorator implements ListeningScheduledExecutorService { final ScheduledExecutorService delegate; ScheduledListeningDecorator(ScheduledExecutorService scheduledExecutorService) { super(scheduledExecutorService); this.delegate = (ScheduledExecutorService) Preconditions.checkNotNull(scheduledExecutorService); } @Override // java.util.concurrent.ScheduledExecutorService public final ListenableScheduledFuture schedule(Runnable runnable, long j, TimeUnit timeUnit) { TrustedListenableFutureTask create = TrustedListenableFutureTask.create(runnable, null); return new ListenableScheduledTask(create, this.delegate.schedule(create, j, timeUnit)); } @Override // java.util.concurrent.ScheduledExecutorService public final ListenableScheduledFuture schedule(Callable callable, long j, TimeUnit timeUnit) { TrustedListenableFutureTask create = TrustedListenableFutureTask.create(callable); return new ListenableScheduledTask(create, this.delegate.schedule(create, j, timeUnit)); } @Override // java.util.concurrent.ScheduledExecutorService public final ListenableScheduledFuture scheduleAtFixedRate(Runnable runnable, long j, long j2, TimeUnit timeUnit) { NeverSuccessfulListenableFutureTask neverSuccessfulListenableFutureTask = new NeverSuccessfulListenableFutureTask(runnable); return new ListenableScheduledTask(neverSuccessfulListenableFutureTask, this.delegate.scheduleAtFixedRate(neverSuccessfulListenableFutureTask, j, j2, timeUnit)); } @Override // java.util.concurrent.ScheduledExecutorService public final ListenableScheduledFuture scheduleWithFixedDelay(Runnable runnable, long j, long j2, TimeUnit timeUnit) { NeverSuccessfulListenableFutureTask neverSuccessfulListenableFutureTask = new NeverSuccessfulListenableFutureTask(runnable); return new ListenableScheduledTask(neverSuccessfulListenableFutureTask, this.delegate.scheduleWithFixedDelay(neverSuccessfulListenableFutureTask, j, j2, timeUnit)); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static final class ListenableScheduledTask extends ForwardingListenableFuture.SimpleForwardingListenableFuture implements ListenableScheduledFuture { private final ScheduledFuture scheduledDelegate; public ListenableScheduledTask(ListenableFuture listenableFuture, ScheduledFuture scheduledFuture) { super(listenableFuture); this.scheduledDelegate = scheduledFuture; } @Override // com.google.common.util.concurrent.ForwardingFuture, java.util.concurrent.Future public final boolean cancel(boolean z) { boolean cancel = super.cancel(z); if (cancel) { this.scheduledDelegate.cancel(z); } return cancel; } @Override // java.util.concurrent.Delayed public final long getDelay(TimeUnit timeUnit) { return this.scheduledDelegate.getDelay(timeUnit); } @Override // java.lang.Comparable public final int compareTo(Delayed delayed) { return this.scheduledDelegate.compareTo(delayed); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static final class NeverSuccessfulListenableFutureTask extends AbstractFuture.TrustedFuture implements Runnable { private final Runnable delegate; public NeverSuccessfulListenableFutureTask(Runnable runnable) { this.delegate = (Runnable) Preconditions.checkNotNull(runnable); } @Override // java.lang.Runnable public final void run() { try { this.delegate.run(); } catch (Throwable th) { setException(th); throw Throwables.propagate(th); } } } } /* JADX WARN: Removed duplicated region for block: B:19:0x0091 */ /* JADX WARN: Removed duplicated region for block: B:47:0x00b6 A[SYNTHETIC] */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ static T invokeAnyImpl(com.google.common.util.concurrent.ListeningExecutorService r16, java.util.Collection> r17, boolean r18, long r19, java.util.concurrent.TimeUnit r21) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException { /* r1 = r16 com.google.common.base.Preconditions.checkNotNull(r16) com.google.common.base.Preconditions.checkNotNull(r21) int r0 = r17.size() r2 = 1 if (r0 <= 0) goto L11 r3 = r2 goto L12 L11: r3 = 0 L12: com.google.common.base.Preconditions.checkArgument(r3) java.util.ArrayList r3 = com.google.common.collect.Lists.newArrayListWithCapacity(r0) java.util.concurrent.LinkedBlockingQueue r4 = com.google.common.collect.Queues.newLinkedBlockingQueue() r5 = r19 r7 = r21 long r5 = r7.toNanos(r5) if (r18 == 0) goto L2f long r7 = java.lang.System.nanoTime() // Catch: java.lang.Throwable -> L2c goto L31 L2c: r0 = move-exception goto Lba L2f: r7 = 0 L31: java.util.Iterator r9 = r17.iterator() // Catch: java.lang.Throwable -> L2c java.lang.Object r10 = r9.next() // Catch: java.lang.Throwable -> L2c java.util.concurrent.Callable r10 = (java.util.concurrent.Callable) r10 // Catch: java.lang.Throwable -> L2c com.google.common.util.concurrent.ListenableFuture r10 = submitAndAddQueueListener(r1, r10, r4) // Catch: java.lang.Throwable -> L2c r3.add(r10) // Catch: java.lang.Throwable -> L2c int r0 = r0 - r2 r10 = 0 r11 = r2 r12 = r10 L46: java.lang.Object r13 = r4.poll() // Catch: java.lang.Throwable -> L2c java.util.concurrent.Future r13 = (java.util.concurrent.Future) r13 // Catch: java.lang.Throwable -> L2c if (r13 != 0) goto L8c if (r0 <= 0) goto L62 int r0 = r0 + (-1) java.lang.Object r14 = r9.next() // Catch: java.lang.Throwable -> L2c java.util.concurrent.Callable r14 = (java.util.concurrent.Callable) r14 // Catch: java.lang.Throwable -> L2c com.google.common.util.concurrent.ListenableFuture r14 = submitAndAddQueueListener(r1, r14, r4) // Catch: java.lang.Throwable -> L2c r3.add(r14) // Catch: java.lang.Throwable -> L2c int r11 = r11 + 1 goto L8c L62: if (r11 != 0) goto L6c if (r12 != 0) goto L6b java.util.concurrent.ExecutionException r12 = new java.util.concurrent.ExecutionException // Catch: java.lang.Throwable -> L2c r12.(r10) // Catch: java.lang.Throwable -> L2c L6b: throw r12 // Catch: java.lang.Throwable -> L2c L6c: if (r18 == 0) goto L86 java.util.concurrent.TimeUnit r13 = java.util.concurrent.TimeUnit.NANOSECONDS // Catch: java.lang.Throwable -> L2c java.lang.Object r13 = r4.poll(r5, r13) // Catch: java.lang.Throwable -> L2c java.util.concurrent.Future r13 = (java.util.concurrent.Future) r13 // Catch: java.lang.Throwable -> L2c if (r13 == 0) goto L80 long r14 = java.lang.System.nanoTime() // Catch: java.lang.Throwable -> L2c long r7 = r14 - r7 long r5 = r5 - r7 goto L8d L80: java.util.concurrent.TimeoutException r0 = new java.util.concurrent.TimeoutException // Catch: java.lang.Throwable -> L2c r0.() // Catch: java.lang.Throwable -> L2c throw r0 // Catch: java.lang.Throwable -> L2c L86: java.lang.Object r13 = r4.take() // Catch: java.lang.Throwable -> L2c java.util.concurrent.Future r13 = (java.util.concurrent.Future) r13 // Catch: java.lang.Throwable -> L2c L8c: r14 = r7 L8d: r6 = r5 r5 = r0 if (r13 == 0) goto Lb6 int r11 = r11 + (-1) java.lang.Object r0 = r13.get() // Catch: java.lang.Throwable -> L2c java.lang.RuntimeException -> Lac java.util.concurrent.ExecutionException -> Lb4 java.util.Iterator r1 = r3.iterator() L9b: boolean r3 = r1.hasNext() if (r3 == 0) goto Lab java.lang.Object r3 = r1.next() java.util.concurrent.Future r3 = (java.util.concurrent.Future) r3 r3.cancel(r2) goto L9b Lab: return r0 Lac: r0 = move-exception r8 = r0 java.util.concurrent.ExecutionException r12 = new java.util.concurrent.ExecutionException // Catch: java.lang.Throwable -> L2c r12.(r8) // Catch: java.lang.Throwable -> L2c goto Lb6 Lb4: r0 = move-exception r12 = r0 Lb6: r0 = r5 r5 = r6 r7 = r14 goto L46 Lba: java.util.Iterator r1 = r3.iterator() Lbe: boolean r3 = r1.hasNext() if (r3 == 0) goto Lce java.lang.Object r3 = r1.next() java.util.concurrent.Future r3 = (java.util.concurrent.Future) r3 r3.cancel(r2) goto Lbe Lce: throw r0 */ throw new UnsupportedOperationException("Method not decompiled: com.google.common.util.concurrent.MoreExecutors.invokeAnyImpl(com.google.common.util.concurrent.ListeningExecutorService, java.util.Collection, boolean, long, java.util.concurrent.TimeUnit):java.lang.Object"); } private static ListenableFuture submitAndAddQueueListener(ListeningExecutorService listeningExecutorService, Callable callable, BlockingQueue> blockingQueue) { ListenableFuture submit = listeningExecutorService.submit((Callable) callable); submit.addListener(new Runnable(blockingQueue, submit) { // from class: com.google.common.util.concurrent.MoreExecutors.1 final ListenableFuture val$future; final BlockingQueue val$queue; { this.val$queue = blockingQueue; this.val$future = submit; } @Override // java.lang.Runnable public void run() { this.val$queue.add(this.val$future); } }, directExecutor()); return submit; } public static ThreadFactory platformThreadFactory() { if (!isAppEngineWithApiClasses()) { return Executors.defaultThreadFactory(); } try { return (ThreadFactory) Class.forName("com.google.appengine.api.ThreadManager").getMethod("currentRequestThreadFactory", new Class[0]).invoke(null, new Object[0]); } catch (ClassNotFoundException e) { throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e); } catch (IllegalAccessException e2) { throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e2); } catch (NoSuchMethodException e3) { throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e3); } catch (InvocationTargetException e4) { throw Throwables.propagate(e4.getCause()); } } private static boolean isAppEngineWithApiClasses() { if (System.getProperty("com.google.appengine.runtime.environment") == null) { return false; } try { Class.forName("com.google.appengine.api.utils.SystemProperty"); return Class.forName("com.google.apphosting.api.ApiProxy").getMethod("getCurrentEnvironment", new Class[0]).invoke(null, new Object[0]) != null; } catch (ClassNotFoundException | IllegalAccessException | NoSuchMethodException | InvocationTargetException unused) { return false; } } /* JADX INFO: Access modifiers changed from: package-private */ public static Thread newThread(String str, Runnable runnable) { Preconditions.checkNotNull(str); Preconditions.checkNotNull(runnable); Thread newThread = platformThreadFactory().newThread(runnable); try { newThread.setName(str); } catch (SecurityException unused) { } return newThread; } /* JADX INFO: Access modifiers changed from: package-private */ public static Executor renamingDecorator(Executor executor, Supplier supplier) { Preconditions.checkNotNull(executor); Preconditions.checkNotNull(supplier); return new Executor(executor, supplier) { // from class: com.google.common.util.concurrent.MoreExecutors.2 final Executor val$executor; final Supplier val$nameSupplier; { this.val$executor = executor; this.val$nameSupplier = supplier; } @Override // java.util.concurrent.Executor public void execute(Runnable runnable) { this.val$executor.execute(Callables.threadRenaming(runnable, (Supplier) this.val$nameSupplier)); } }; } static ExecutorService renamingDecorator(ExecutorService executorService, Supplier supplier) { Preconditions.checkNotNull(executorService); Preconditions.checkNotNull(supplier); return new WrappingExecutorService(executorService, supplier) { // from class: com.google.common.util.concurrent.MoreExecutors.3 final Supplier val$nameSupplier; { this.val$nameSupplier = supplier; } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.util.concurrent.WrappingExecutorService public Callable wrapTask(Callable callable) { return Callables.threadRenaming(callable, (Supplier) this.val$nameSupplier); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.util.concurrent.WrappingExecutorService public Runnable wrapTask(Runnable runnable) { return Callables.threadRenaming(runnable, (Supplier) this.val$nameSupplier); } }; } /* JADX INFO: Access modifiers changed from: package-private */ public static ScheduledExecutorService renamingDecorator(ScheduledExecutorService scheduledExecutorService, Supplier supplier) { Preconditions.checkNotNull(scheduledExecutorService); Preconditions.checkNotNull(supplier); return new WrappingScheduledExecutorService(scheduledExecutorService, supplier) { // from class: com.google.common.util.concurrent.MoreExecutors.4 final Supplier val$nameSupplier; { this.val$nameSupplier = supplier; } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.util.concurrent.WrappingExecutorService public Callable wrapTask(Callable callable) { return Callables.threadRenaming(callable, (Supplier) this.val$nameSupplier); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.util.concurrent.WrappingExecutorService public Runnable wrapTask(Runnable runnable) { return Callables.threadRenaming(runnable, (Supplier) this.val$nameSupplier); } }; } public static boolean shutdownAndAwaitTermination(ExecutorService executorService, long j, TimeUnit timeUnit) { long nanos = timeUnit.toNanos(j) / 2; executorService.shutdown(); try { if (!executorService.awaitTermination(nanos, TimeUnit.NANOSECONDS)) { executorService.shutdownNow(); executorService.awaitTermination(nanos, TimeUnit.NANOSECONDS); } } catch (InterruptedException unused) { Thread.currentThread().interrupt(); executorService.shutdownNow(); } return executorService.isTerminated(); } /* JADX INFO: Access modifiers changed from: package-private */ public static Executor rejectionPropagatingExecutor(Executor executor, AbstractFuture abstractFuture) { Preconditions.checkNotNull(executor); Preconditions.checkNotNull(abstractFuture); return executor == directExecutor() ? executor : new Executor(executor, abstractFuture) { // from class: com.google.common.util.concurrent.MoreExecutors.5 final Executor val$delegate; final AbstractFuture val$future; { this.val$delegate = executor; this.val$future = abstractFuture; } @Override // java.util.concurrent.Executor public void execute(Runnable runnable) { try { this.val$delegate.execute(runnable); } catch (RejectedExecutionException e) { this.val$future.setException(e); } } }; } }