discord-jadx/app/src/main/java/androidx/work/impl/utils/SerialExecutor.java
2021-07-24 04:37:17 +02:00

69 lines
1.9 KiB
Java

package androidx.work.impl.utils;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import java.util.ArrayDeque;
import java.util.concurrent.Executor;
public class SerialExecutor implements Executor {
private volatile Runnable mActive;
private final Executor mExecutor;
private final Object mLock = new Object();
private final ArrayDeque<Task> mTasks = new ArrayDeque<>();
public static class Task implements Runnable {
public final Runnable mRunnable;
public final SerialExecutor mSerialExecutor;
public Task(@NonNull SerialExecutor serialExecutor, @NonNull Runnable runnable) {
this.mSerialExecutor = serialExecutor;
this.mRunnable = runnable;
}
@Override // java.lang.Runnable
public void run() {
try {
this.mRunnable.run();
} finally {
this.mSerialExecutor.scheduleNext();
}
}
}
public SerialExecutor(@NonNull Executor executor) {
this.mExecutor = executor;
}
@Override // java.util.concurrent.Executor
public void execute(@NonNull Runnable runnable) {
synchronized (this.mLock) {
this.mTasks.add(new Task(this, runnable));
if (this.mActive == null) {
scheduleNext();
}
}
}
@NonNull
@VisibleForTesting
public Executor getDelegatedExecutor() {
return this.mExecutor;
}
public boolean hasPendingTasks() {
boolean z2;
synchronized (this.mLock) {
z2 = !this.mTasks.isEmpty();
}
return z2;
}
public void scheduleNext() {
synchronized (this.mLock) {
Task poll = this.mTasks.poll();
this.mActive = poll;
if (poll != null) {
this.mExecutor.execute(this.mActive);
}
}
}
}