package o;

import java.util.ArrayList;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Logger;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class Uc0 {
    public static final BP h = new BP(20);
    public static final Uc0 i = new Uc0(new WT(new ThreadFactoryC2169pi0(AbstractC2462sn.n(new StringBuilder(), AbstractC2264qi0.g, " TaskRunner"), true)));
    public static final Logger j = Logger.getLogger(Uc0.class.getName());
    public final WT a;
    public boolean c;
    public long d;
    public int b = 10000;
    public final ArrayList e = new ArrayList();
    public final ArrayList f = new ArrayList();
    public final RunnableC2017o3 g = new RunnableC2017o3(17, this);

    public Uc0(WT wt) {
        this.a = wt;
    }

    public static final void a(Uc0 uc0, Oc0 oc0) {
        byte[] bArr = AbstractC2264qi0.a;
        Thread threadCurrentThread = Thread.currentThread();
        String name = threadCurrentThread.getName();
        threadCurrentThread.setName(oc0.a);
        try {
            long jA = oc0.a();
            synchronized (uc0) {
                uc0.b(oc0, jA);
            }
            threadCurrentThread.setName(name);
        } catch (Throwable th) {
            synchronized (uc0) {
                uc0.b(oc0, -1L);
                threadCurrentThread.setName(name);
                throw th;
            }
        }
    }

    public final void b(Oc0 oc0, long j2) {
        byte[] bArr = AbstractC2264qi0.a;
        Tc0 tc0 = oc0.c;
        if (tc0.d != oc0) {
            throw new IllegalStateException("Check failed.");
        }
        boolean z = tc0.f;
        tc0.f = false;
        tc0.d = null;
        this.e.remove(tc0);
        if (j2 != -1 && !z && !tc0.c) {
            tc0.d(oc0, j2, true);
        }
        if (tc0.e.isEmpty()) {
            return;
        }
        this.f.add(tc0);
    }

    public final Oc0 c() {
        long j2;
        Oc0 oc0;
        boolean z;
        byte[] bArr = AbstractC2264qi0.a;
        while (true) {
            ArrayList arrayList = this.f;
            if (arrayList.isEmpty()) {
                return null;
            }
            long jNanoTime = System.nanoTime();
            int size = arrayList.size();
            long jMin = Long.MAX_VALUE;
            int i2 = 0;
            Oc0 oc02 = null;
            while (true) {
                if (i2 >= size) {
                    j2 = jNanoTime;
                    oc0 = null;
                    z = false;
                    break;
                }
                Object obj = arrayList.get(i2);
                i2++;
                Oc0 oc03 = (Oc0) ((Tc0) obj).e.get(0);
                j2 = jNanoTime;
                oc0 = null;
                long jMax = Math.max(0L, oc03.d - j2);
                if (jMax > 0) {
                    jMin = Math.min(jMax, jMin);
                } else {
                    if (oc02 != null) {
                        z = true;
                        break;
                    }
                    oc02 = oc03;
                }
                jNanoTime = j2;
            }
            ArrayList arrayList2 = this.e;
            if (oc02 != null) {
                byte[] bArr2 = AbstractC2264qi0.a;
                oc02.d = -1L;
                Tc0 tc0 = oc02.c;
                tc0.e.remove(oc02);
                arrayList.remove(tc0);
                tc0.d = oc02;
                arrayList2.add(tc0);
                if (z || (!this.c && !arrayList.isEmpty())) {
                    ((ThreadPoolExecutor) this.a.m).execute(this.g);
                }
                return oc02;
            }
            if (this.c) {
                if (jMin >= this.d - j2) {
                    return oc0;
                }
                notify();
                return oc0;
            }
            this.c = true;
            this.d = j2 + jMin;
            try {
                try {
                    long j3 = jMin / 1000000;
                    long j4 = jMin - (1000000 * j3);
                    if (j3 > 0 || jMin > 0) {
                        wait(j3, (int) j4);
                    }
                } catch (InterruptedException unused) {
                    for (int size2 = arrayList2.size() - 1; -1 < size2; size2--) {
                        ((Tc0) arrayList2.get(size2)).b();
                    }
                    for (int size3 = arrayList.size() - 1; -1 < size3; size3--) {
                        Tc0 tc02 = (Tc0) arrayList.get(size3);
                        tc02.b();
                        if (tc02.e.isEmpty()) {
                            arrayList.remove(size3);
                        }
                    }
                }
            } finally {
                this.c = false;
            }
        }
    }

    public final void d(Tc0 tc0) {
        byte[] bArr = AbstractC2264qi0.a;
        if (tc0.d == null) {
            boolean zIsEmpty = tc0.e.isEmpty();
            ArrayList arrayList = this.f;
            if (zIsEmpty) {
                arrayList.remove(tc0);
            } else if (!arrayList.contains(tc0)) {
                arrayList.add(tc0);
            }
        }
        if (this.c) {
            notify();
        } else {
            ((ThreadPoolExecutor) this.a.m).execute(this.g);
        }
    }

    public final Tc0 e() {
        int i2;
        synchronized (this) {
            i2 = this.b;
            this.b = i2 + 1;
        }
        return new Tc0(this, AbstractC0126Ew.j(i2, "Q"));
    }
}
