一个不会崩溃的线程处理工具

作者: ReadyShow | 来源:发表于2019-12-17 19:34 被阅读0次
    
    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);
        }
    }
    
    

    相关文章

      网友评论

        本文标题:一个不会崩溃的线程处理工具

        本文链接:https://www.haomeiwen.com/subject/gjsenctx.html