what-the-bank/sources/com/google/common/util/concurrent/InterruptibleTask.java

152 lines
5.3 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.util.concurrent;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
/* loaded from: classes2.dex */
abstract class InterruptibleTask<T> extends AtomicReference<Runnable> implements Runnable {
private static final Runnable DONE;
private static final Runnable INTERRUPTING;
private static final int MAX_BUSY_WAIT_SPINS = 1000;
private static final Runnable PARKED;
abstract void afterRanInterruptibly(T t, Throwable th);
abstract boolean isDone();
abstract T runInterruptibly() throws Exception;
abstract String toPendingString();
/* loaded from: classes2.dex */
static final class DoNothingRunnable implements Runnable {
@Override // java.lang.Runnable
public final void run() {
}
private DoNothingRunnable() {
}
}
static {
DONE = new DoNothingRunnable();
INTERRUPTING = new DoNothingRunnable();
PARKED = new DoNothingRunnable();
}
@Override // java.lang.Runnable
public final void run() {
T runInterruptibly;
Thread currentThread = Thread.currentThread();
if (compareAndSet(null, currentThread)) {
boolean z = !isDone();
if (z) {
try {
runInterruptibly = runInterruptibly();
} catch (Throwable th) {
if (!compareAndSet(currentThread, DONE)) {
Runnable runnable = get();
boolean z2 = false;
int i = 0;
while (true) {
Runnable runnable2 = INTERRUPTING;
if (runnable != runnable2 && runnable != PARKED) {
break;
}
i++;
if (i > 1000) {
Runnable runnable3 = PARKED;
if (runnable == runnable3 || compareAndSet(runnable2, runnable3)) {
z2 = Thread.interrupted() || z2;
LockSupport.park(this);
}
} else {
Thread.yield();
}
runnable = get();
}
if (z2) {
currentThread.interrupt();
}
}
if (z) {
afterRanInterruptibly(null, th);
return;
}
return;
}
} else {
runInterruptibly = null;
}
if (!compareAndSet(currentThread, DONE)) {
Runnable runnable4 = get();
boolean z3 = false;
int i2 = 0;
while (true) {
Runnable runnable5 = INTERRUPTING;
if (runnable4 != runnable5 && runnable4 != PARKED) {
break;
}
i2++;
if (i2 > 1000) {
Runnable runnable6 = PARKED;
if (runnable4 == runnable6 || compareAndSet(runnable5, runnable6)) {
z3 = Thread.interrupted() || z3;
LockSupport.park(this);
}
} else {
Thread.yield();
}
runnable4 = get();
}
if (z3) {
currentThread.interrupt();
}
}
if (z) {
afterRanInterruptibly(runInterruptibly, null);
}
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public final void interruptTask() {
Runnable runnable = get();
if ((runnable instanceof Thread) && compareAndSet(runnable, INTERRUPTING)) {
try {
((Thread) runnable).interrupt();
} finally {
if (getAndSet(DONE) == PARKED) {
LockSupport.unpark((Thread) runnable);
}
}
}
}
@Override // java.util.concurrent.atomic.AtomicReference
public final String toString() {
String str;
Runnable runnable = get();
if (runnable == DONE) {
str = "running=[DONE]";
} else if (runnable == INTERRUPTING) {
str = "running=[INTERRUPTED]";
} else if (runnable instanceof Thread) {
String name = ((Thread) runnable).getName();
StringBuilder sb = new StringBuilder(String.valueOf(name).length() + 21);
sb.append("running=[RUNNING ON ");
sb.append(name);
sb.append("]");
str = sb.toString();
} else {
str = "running=[NOT STARTED YET]";
}
String pendingString = toPendingString();
StringBuilder sb2 = new StringBuilder(String.valueOf(str).length() + 2 + String.valueOf(pendingString).length());
sb2.append(str);
sb2.append(", ");
sb2.append(pendingString);
return sb2.toString();
}
}