jimu-decompiled/sources/androidx/core/provider/SelfDestructiveThread.java
2025-05-13 19:24:51 +02:00

143 lines
4.6 KiB
Java

package androidx.core.provider;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/* loaded from: classes.dex */
public class SelfDestructiveThread {
private HandlerThread b;
private Handler c;
private final int f;
private final int g;
private final String h;
private final Object a = new Object();
private Handler.Callback e = new Handler.Callback() { // from class: androidx.core.provider.SelfDestructiveThread.1
@Override // android.os.Handler.Callback
public boolean handleMessage(Message message) {
int i = message.what;
if (i == 0) {
SelfDestructiveThread.this.a();
return true;
}
if (i != 1) {
return true;
}
SelfDestructiveThread.this.a((Runnable) message.obj);
return true;
}
};
private int d = 0;
public interface ReplyCallback<T> {
void a(T t);
}
public SelfDestructiveThread(String str, int i, int i2) {
this.h = str;
this.g = i;
this.f = i2;
}
private void b(Runnable runnable) {
synchronized (this.a) {
if (this.b == null) {
this.b = new HandlerThread(this.h, this.g);
this.b.start();
this.c = new Handler(this.b.getLooper(), this.e);
this.d++;
}
this.c.removeMessages(0);
this.c.sendMessage(this.c.obtainMessage(1, runnable));
}
}
public <T> void a(final Callable<T> callable, final ReplyCallback<T> replyCallback) {
final Handler handler = new Handler();
b(new Runnable(this) { // from class: androidx.core.provider.SelfDestructiveThread.2
@Override // java.lang.Runnable
public void run() {
final Object obj;
try {
obj = callable.call();
} catch (Exception unused) {
obj = null;
}
handler.post(new Runnable() { // from class: androidx.core.provider.SelfDestructiveThread.2.1
@Override // java.lang.Runnable
public void run() {
replyCallback.a(obj);
}
});
}
});
}
public <T> T a(final Callable<T> callable, int i) throws InterruptedException {
final ReentrantLock reentrantLock = new ReentrantLock();
final Condition newCondition = reentrantLock.newCondition();
final AtomicReference atomicReference = new AtomicReference();
final AtomicBoolean atomicBoolean = new AtomicBoolean(true);
b(new Runnable(this) { // from class: androidx.core.provider.SelfDestructiveThread.3
@Override // java.lang.Runnable
public void run() {
try {
atomicReference.set(callable.call());
} catch (Exception unused) {
}
reentrantLock.lock();
try {
atomicBoolean.set(false);
newCondition.signal();
} finally {
reentrantLock.unlock();
}
}
});
reentrantLock.lock();
try {
if (!atomicBoolean.get()) {
return (T) atomicReference.get();
}
long nanos = TimeUnit.MILLISECONDS.toNanos(i);
do {
try {
nanos = newCondition.awaitNanos(nanos);
} catch (InterruptedException unused) {
}
if (!atomicBoolean.get()) {
return (T) atomicReference.get();
}
} while (nanos > 0);
throw new InterruptedException("timeout");
} finally {
reentrantLock.unlock();
}
}
void a(Runnable runnable) {
runnable.run();
synchronized (this.a) {
this.c.removeMessages(0);
this.c.sendMessageDelayed(this.c.obtainMessage(0), this.f);
}
}
void a() {
synchronized (this.a) {
if (this.c.hasMessages(1)) {
return;
}
this.b.quit();
this.b = null;
this.c = null;
}
}
}