package com.ironsource.environment.workerthread; import com.ironsource.environment.workerthread.WorkerResult; import com.ironsource.i9; import com.ironsource.mediationsdk.logger.IronLog; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /* loaded from: classes4.dex */ public class WorkerManager { List> a = new ArrayList(); private final ExecutorService b; public interface WorkEndedListener { void onWorkCompleted(List> list, long j); void onWorkFailed(String str); } public WorkerManager(ExecutorService executorService) { this.b = executorService; } public void addCallable(Callable callable) { this.a.add(callable); } public void startWork(WorkEndedListener workEndedListener, long j, TimeUnit timeUnit) { WorkerResult.Canceled canceled; if (this.b.isShutdown()) { workEndedListener.onWorkFailed("can not start work, executor has been shut down"); return; } if (this.a.isEmpty()) { workEndedListener.onWorkFailed("can not start work, callable list is empty"); return; } long currentTimeMillis = System.currentTimeMillis(); ArrayList arrayList = new ArrayList(); try { List> invokeAll = this.b.invokeAll(this.a, j, timeUnit); for (int i = 0; i < invokeAll.size(); i++) { Future future = invokeAll.get(i); if (!future.isDone() || future.isCancelled()) { canceled = new WorkerResult.Canceled(this.a.get(i)); } else { try { arrayList.add(new WorkerResult.Completed(future.get())); } catch (InterruptedException e) { e = e; i9.d().a(e); arrayList.add(new WorkerResult.Failed(this.a.get(i), e)); } catch (CancellationException e2) { i9.d().a(e2); canceled = new WorkerResult.Canceled(this.a.get(i)); } catch (ExecutionException e3) { e = e3; i9.d().a(e); arrayList.add(new WorkerResult.Failed(this.a.get(i), e)); } } arrayList.add(canceled); } workEndedListener.onWorkCompleted(arrayList, System.currentTimeMillis() - currentTimeMillis); this.b.shutdownNow(); } catch (Exception e4) { i9.d().a(e4); IronLog.INTERNAL.error(e4.toString()); workEndedListener.onWorkFailed("failed to invoke callables, error= " + e4.getMessage()); this.b.shutdownNow(); } } }