在Android中我可以设置Handler(looper)优先级吗?

时间:2018-02-12 20:41:06

标签: android multithreading progress-bar android-looper

我有这样的设置,Progressbar显示不确定的进度。然后我将这个背景线程在主looper线程上显示为RecyclerView适配器。

线程同时向具有自己的守护程序线程侦听的Progressbar报告进度,这反过来表现在looper上以显示/设置Progressbar状态。

我的问题/问题是当Progressbar守护程序线程调用looper更新Progressbar时,进度条可视化不顺畅。

我想知道是否在使用Handler时使用Progressbar向主Looper发布,如果我可以像创建普通线程时那样使用优先级,那么SELECT t2.cvid, t2.cid, t1a.extid, t1b.extid, t1c.extid, t1d.extid FROM Table2 t2 INNER JOIN Table1 t1a ON t1a.cvid = t2.cvid and t1a.cid = t2.cid AND t1a.qid = 11 INNER JOIN Table1 t1b ON t1b.cvit1bd = t2.cvid and t1b.cid = t2.cid AND t1b.qid = 21 INNER JOIN Table1 t1c ON t1c.cvid = t2.cvid and t1c.cid = t2.cid AND t1c.qid = 31 INNER JOIN Table1 t1d ON t1d.cvid = t2.cvid and t1d.cid = t2.cid AND t1d.qid = 41 是顺畅的

1 个答案:

答案 0 :(得分:2)

我想这是一个答案,我正在尝试使用'ofThread.setPriority()'作为@pskink评论并且我认为它正在工作。在这里,我设置了MIN_PRIORITY,它减慢了一切,几乎慢了。 3的优先级更好。我认为这应该在运行时根据平台CPU

设置

类:BackgroundExecutorService.java

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;


import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

    public enum BackgroundExecutorService {
        INSTANCE;

        /*
         * Max single thread ExecutorService that will spin down thread after use
         */
        private final Executor executor;

        {
            ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1,
                    5L, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>(),
                    new ThreadFactory() {
                        @Override
                        public Thread newThread(@NonNull final Runnable r) {
                            Thread t = new Thread(r, "GooglePlaceExc" + "Thread");
                            t.setPriority(Thread.MIN_PRIORITY);
                            return t;
                        }
                    });
            executor.allowCoreThreadTimeOut(true);
            this.executor = executor;
        }

        private final Handler mHandler = new Handler(Looper.getMainLooper());

        public <R> void enqueue(final BackgroundJob<R> job) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        final R result = job.executeInBackground();
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                job.onSuccess(result);
                            }
                        });
                    } catch (final Exception e) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                job.onFailure(e);
                            }
                        });
                    }
                }
            });
        }
    }