package com.google.firebase.crashlytics.internal.common; import com.google.firebase.crashlytics.internal.Logger; import java.util.Locale; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; /* loaded from: classes.dex */ public final class ExecutorUtils { private static final long DEFAULT_TERMINATION_TIMEOUT = 2; private ExecutorUtils() { } public static ExecutorService buildSingleThreadExecutorService(String str) { ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor(getNamedThreadFactory(str)); addDelayedShutdownHook(str, newSingleThreadExecutor); return newSingleThreadExecutor; } public static ScheduledExecutorService buildSingleThreadScheduledExecutorService(String str) { ScheduledExecutorService newSingleThreadScheduledExecutor = Executors.newSingleThreadScheduledExecutor(getNamedThreadFactory(str)); addDelayedShutdownHook(str, newSingleThreadScheduledExecutor); return newSingleThreadScheduledExecutor; } public static final ThreadFactory getNamedThreadFactory(String str) { return new ThreadFactory(str, new AtomicLong(1L)) { // from class: com.google.firebase.crashlytics.internal.common.ExecutorUtils.1 final AtomicLong val$count; final String val$threadNameTemplate; { this.val$threadNameTemplate = str; this.val$count = r2; } @Override // java.util.concurrent.ThreadFactory public Thread newThread(Runnable runnable) { Thread newThread = Executors.defaultThreadFactory().newThread(new BackgroundPriorityRunnable(this, runnable) { // from class: com.google.firebase.crashlytics.internal.common.ExecutorUtils.1.1 final AnonymousClass1 this$0; final Runnable val$runnable; { this.this$0 = this; this.val$runnable = runnable; } @Override // com.google.firebase.crashlytics.internal.common.BackgroundPriorityRunnable public void onRun() { this.val$runnable.run(); } }); StringBuilder sb = new StringBuilder(); sb.append(this.val$threadNameTemplate); sb.append(this.val$count.getAndIncrement()); newThread.setName(sb.toString()); return newThread; } }; } private static final void addDelayedShutdownHook(String str, ExecutorService executorService) { addDelayedShutdownHook(str, executorService, DEFAULT_TERMINATION_TIMEOUT, TimeUnit.SECONDS); } public static final void addDelayedShutdownHook(String str, ExecutorService executorService, long j, TimeUnit timeUnit) { Runtime.getRuntime().addShutdownHook(new Thread(new BackgroundPriorityRunnable(str, executorService, j, timeUnit) { // from class: com.google.firebase.crashlytics.internal.common.ExecutorUtils.2 final ExecutorService val$service; final String val$serviceName; final long val$terminationTimeout; final TimeUnit val$timeUnit; { this.val$serviceName = str; this.val$service = executorService; this.val$terminationTimeout = j; this.val$timeUnit = timeUnit; } @Override // com.google.firebase.crashlytics.internal.common.BackgroundPriorityRunnable public void onRun() { try { Logger logger = Logger.getLogger(); StringBuilder sb = new StringBuilder("Executing shutdown hook for "); sb.append(this.val$serviceName); logger.d(sb.toString()); this.val$service.shutdown(); if (this.val$service.awaitTermination(this.val$terminationTimeout, this.val$timeUnit)) { return; } Logger logger2 = Logger.getLogger(); StringBuilder sb2 = new StringBuilder(); sb2.append(this.val$serviceName); sb2.append(" did not shut down in the allocated time. Requesting immediate shutdown."); logger2.d(sb2.toString()); this.val$service.shutdownNow(); } catch (InterruptedException unused) { Logger.getLogger().d(String.format(Locale.US, "Interrupted while waiting for %s to shut down. Requesting immediate shutdown.", this.val$serviceName)); this.val$service.shutdownNow(); } } }, "Crashlytics Shutdown Hook for ".concat(String.valueOf(str)))); } }