package m4;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.logging.Level;
import java.util.logging.Logger;

/* JADX INFO: loaded from: classes3.dex */
public final class Z1 implements Executor, Runnable {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Logger f20920d = Logger.getLogger(Z1.class.getName());

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final c2 f20921e;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Executor f20922a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ConcurrentLinkedQueue f20923b = new ConcurrentLinkedQueue();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public volatile int f20924c = 0;

    static {
        c2 y12;
        try {
            y12 = new X1(AtomicIntegerFieldUpdater.newUpdater(Z1.class, "c"));
        } catch (Throwable th) {
            f20920d.log(Level.SEVERE, "FieldUpdaterAtomicHelper failed", th);
            y12 = new Y1();
        }
        f20921e = y12;
    }

    public Z1(Executor executor) {
        A4.g.o(executor, "'executor' must not be null.");
        this.f20922a = executor;
    }

    public final void a(Runnable runnable) {
        c2 c2Var = f20921e;
        if (c2Var.s(this)) {
            try {
                this.f20922a.execute(this);
            } catch (Throwable th) {
                if (runnable != null) {
                    this.f20923b.remove(runnable);
                }
                c2Var.t(this);
                throw th;
            }
        }
    }

    @Override // java.util.concurrent.Executor
    public final void execute(Runnable runnable) {
        ConcurrentLinkedQueue concurrentLinkedQueue = this.f20923b;
        A4.g.o(runnable, "'r' must not be null.");
        concurrentLinkedQueue.add(runnable);
        a(runnable);
    }

    @Override // java.lang.Runnable
    public final void run() {
        ConcurrentLinkedQueue concurrentLinkedQueue;
        c2 c2Var = f20921e;
        while (true) {
            concurrentLinkedQueue = this.f20923b;
            try {
                Runnable runnable = (Runnable) concurrentLinkedQueue.poll();
                if (runnable == null) {
                    break;
                }
                try {
                    runnable.run();
                } catch (RuntimeException e6) {
                    f20920d.log(Level.SEVERE, "Exception while executing runnable " + runnable, (Throwable) e6);
                }
            } catch (Throwable th) {
                c2Var.t(this);
                throw th;
            }
        }
        c2Var.t(this);
        if (concurrentLinkedQueue.isEmpty()) {
            return;
        }
        a(null);
    }
}
