一个不会崩溃的线程处理工具
2019-12-17 本文已影响0人
ReadyShow
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import com.youku.service.download.util.TlogUtils;
public class AsyncHandlerThread {
public static final AsyncHandlerThread INSTANCE = new AsyncHandlerThread();
private HandlerThread thread;
private AsyncHandlerThread() {
try {
thread = new HandlerThread("AsyncHandlerThread");
thread.start();
} catch (InternalError e) {
TlogUtils.loge("异步线程异常");
}
}
public Looper getLooper() {
return thread.getLooper();
}
public static void runSafe(final Runnable r) {
new Handler(INSTANCE.getLooper()).post(new Runnable() {
@Override
public void run() {
try {
r.run();
} catch (Throwable e) {
TlogUtils.loge(e.getMessage());
}
}
});
}
@Deprecated
public static void runHere(final Runnable r) {
runSafe(r);
}
}