在 Android 中我可以设置 Handler(looper) 优先级吗?
In Android can I set the Handler(looper) priority?
我有这个设置,其中 Progressbar
显示不确定的进度。然后我将这个在主循环线程上浮出水面的后台线程放入 RecyclerView 适配器中。
线程同时向 Progressbar
报告进度,它有自己的守护线程侦听,这反过来在循环器上显示 show/set Progressbar
状态。
我的problem/question是Progressbar
守护线程调用looper更新Progressbar
时,进度条可视化不流畅。
我想知道在使用 Handler
发布到 Main Looper 时是否可以像创建普通线程时那样优先排序,以便 Progressbar
顺利
我猜这是一个答案,正如@pskink 所评论的那样,我正在尝试“Thread.setPriority()”,我认为它正在工作。在这里我设置了 MIN_PRIORITY
并且它几乎减慢了一切。优先级为 3 更好。我认为这应该根据平台在运行时设置 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);
}
});
}
}
});
}
}
我有这个设置,其中 Progressbar
显示不确定的进度。然后我将这个在主循环线程上浮出水面的后台线程放入 RecyclerView 适配器中。
线程同时向 Progressbar
报告进度,它有自己的守护线程侦听,这反过来在循环器上显示 show/set Progressbar
状态。
我的problem/question是Progressbar
守护线程调用looper更新Progressbar
时,进度条可视化不流畅。
我想知道在使用 Handler
发布到 Main Looper 时是否可以像创建普通线程时那样优先排序,以便 Progressbar
顺利
我猜这是一个答案,正如@pskink 所评论的那样,我正在尝试“Thread.setPriority()”,我认为它正在工作。在这里我设置了 MIN_PRIORITY
并且它几乎减慢了一切。优先级为 3 更好。我认为这应该根据平台在运行时设置 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);
}
});
}
}
});
}
}