package i.a.n.g;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReferenceArray;

/* JADX INFO: compiled from: ScheduledRunnable.java */
/* JADX INFO: loaded from: classes.dex */
public final class j extends AtomicReferenceArray<Object> implements Runnable, Callable<Object>, i.a.k.b {
    static final Object d = new Object();
    static final Object e = new Object();
    static final Object f = new Object();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    static final Object f875g = new Object();
    final Runnable b;

    public j(Runnable runnable, i.a.n.a.a aVar) {
        super(3);
        this.b = runnable;
        lazySet(0, aVar);
    }

    public void a(Future<?> future) {
        Object obj;
        do {
            obj = get(1);
            if (obj == f875g) {
                return;
            }
            if (obj == e) {
                future.cancel(false);
                return;
            } else if (obj == f) {
                future.cancel(true);
                return;
            }
        } while (!compareAndSet(1, obj, future));
    }

    @Override // java.util.concurrent.Callable
    public Object call() {
        run();
        return null;
    }

    @Override // i.a.k.b
    public void dispose() {
        Object obj;
        Object obj2;
        while (true) {
            Object obj3 = get(1);
            if (obj3 == f875g || obj3 == e || obj3 == f) {
                break;
            }
            boolean z = get(2) != Thread.currentThread();
            if (compareAndSet(1, obj3, z ? f : e)) {
                if (obj3 != null) {
                    ((Future) obj3).cancel(z);
                }
            }
        }
        do {
            obj = get(0);
            if (obj == f875g || obj == (obj2 = d) || obj == null) {
                return;
            }
        } while (!compareAndSet(0, obj, obj2));
        ((i.a.n.a.a) obj).a(this);
    }

    @Override // i.a.k.b
    public boolean isDisposed() {
        Object obj = get(0);
        return obj == d || obj == f875g;
    }

    @Override // java.lang.Runnable
    public void run() {
        Object obj;
        Object obj2;
        Object obj3;
        boolean zCompareAndSet;
        Object obj4;
        lazySet(2, Thread.currentThread());
        try {
            this.b.run();
        } finally {
            try {
            } catch (Throwable th) {
                do {
                    if (obj == obj2) {
                        break;
                    } else if (obj == obj3) {
                        break;
                    }
                } while (!zCompareAndSet);
            }
        }
        lazySet(2, null);
        Object obj5 = get(0);
        if (obj5 != d && compareAndSet(0, obj5, f875g) && obj5 != null) {
            ((i.a.n.a.a) obj5).a(this);
        }
        do {
            obj4 = get(1);
            if (obj4 == e || obj4 == f) {
                return;
            }
        } while (!compareAndSet(1, obj4, f875g));
    }
}
