Search code examples
androidmultithreadingprogress-barandroid-looper

In Android can I set the Handler(looper) priority?


I have this setup where a Progressbar show indetermient progress. Then I have this background Threads that surface on the main looper thread into a RecyclerView adapter.

The Threads simultaneously report progress to the Progressbar that have it´s own daemon thread listening, this in turn surfaces on the looper to show/set the Progressbar status.

My problem/question is that when the Progressbar daemon thread calls the looper to updates the Progressbar, the progress bar visualization is not smooth.

I wonder if when publishing to the Main Looper using a Handler if I could prioritets just like when creating a normal Thread, so that Progressbar is smooth


Solution

  • Here´s an answer I guess, I experimenting with the ´Thread.setPriority()´ as @pskink commented and it´s working I think. Here I set the MIN_PRIORITY and it´s slowing everything down almost to slow. A Priority of 3 is better. I think this should be set during runtime depending on the platform CPU

    Class: 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);
                                }
                            });
                        }
                    }
                });
            }
        }