线程池执行器以及它们在 Android 中的使用。我们将通过大量示例代码彻底介绍这些主题。
虽然现在我们都使用RxJava、Kotlin-Coroutines来执行后台任务,但了解 ThreadPoolExecutor 很重要,因为所有这些库都是在 ThreadPoolExecutor 之上构建的。
线程池
线程池管理一个工作线程池(具体数量取决于其实现)。
任务队列保存等待池中任何空闲线程执行的任务。任务由生产者添加到队列中,而工作线程充当消费者,只要有空闲线程准备好执行新的后台执行,就会从队列中消费任务。
线程池执行器
ThreadPoolExecutor使用线程池中的一个线程执行给定任务。
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue
);
这些参数是什么?
corePoolSize:保留在池中的最小线程数。最初,池中有零个线程。但是随着任务被添加到队列中,新的线程被创建。如果有空闲线程——但线程数低于 corePoolSize——那么新线程将继续被创建。
maximumPoolSize:池中允许的最大线程数。如果这超过了 corePoolSize——并且当前线程数 >= corePoolSize——那么只有当队列已满时才会创建新的工作线程。
keepAliveTime:当线程数大于核心时,非核心线程(多余的空闲线程)会等待一个新的任务,如果在这个参数定义的时间内没有得到一个,就会终止。
unit:keepAliveTime的时间单位。
workQueue:任务队列,只会存放可运行的任务。它必须是一个 BlockingQueue。
为什么在 Android 或 JAVA 应用程序中使用线程池执行器?
它是一个强大的任务执行框架,因为它支持队列中的任务添加、任务取消和任务优先级。
它减少了与线程创建相关的开销,因为它在其线程池中管理所需数量的线程。
在 Android 中使用 ThreadPoolExecutor
首先,创建一个 PriorityThreadFactory:
public class PriorityThreadFactory implements ThreadFactory {
private final int mThreadPriority;
public PriorityThreadFactory(int threadPriority) {
mThreadPriority = threadPriority;
}
@Override
public Thread newThread(final Runnable runnable) {
Runnable wrapperRunnable = new Runnable() {
@Override
public void run() {
try {
Process.setThreadPriority(mThreadPriority);
} catch (Throwable t) {
}
runnable.run();
}
};
return new Thread(wrapperRunnable);
}
}
创建一个 MainThreadExecutor:
public class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override
public void execute(Runnable runnable) {
handler.post(runnable);
}
}
创建一个 DefaultExecutorSupplier:
/*
* Singleton class for default executor supplier
*/
public class DefaultExecutorSupplier{
/*
* Number of cores to decide the number of threads
*/
public static final int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
/*
* thread pool executor for background tasks
*/
private final ThreadPoolExecutor mForBackgroundTasks;
/*
* thread pool executor for light weight background tasks
*/
private final ThreadPoolExecutor mForLightWeightBackgroundTasks;
/*
* thread pool executor for main thread tasks
*/
private final Executor mMainThreadExecutor;
/*
* an instance of DefaultExecutorSupplier
*/
private static DefaultExecutorSupplier sInstance;
/*
* returns the instance of DefaultExecutorSupplier
*/
public static DefaultExecutorSupplier getInstance() {
if (sInstance == null) {
synchronized(DefaultExecutorSupplier.class){
sInstance = new DefaultExecutorSupplier();
}
return sInstance;
}
/*
* constructor for DefaultExecutorSupplier
*/
private DefaultExecutorSupplier() {
// setting the thread factory
ThreadFactory backgroundPriorityThreadFactory = new
PriorityThreadFactory(Process.THREAD_PRIORITY_BACKGROUND);
// setting the thread pool executor for mForBackgroundTasks;
mForBackgroundTasks = new ThreadPoolExecutor(
NUMBER_OF_CORES * 2,
NUMBER_OF_CORES * 2,
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
backgroundPriorityThreadFactory
);
// setting the thread pool executor for mForLightWeightBackgroundTasks;
mForLightWeightBackgroundTasks = new ThreadPoolExecutor(
NUMBER_OF_CORES * 2,
NUMBER_OF_CORES * 2,
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
backgroundPriorityThreadFactory
);
// setting the thread pool executor for mMainThreadExecutor;
mMainThreadExecutor = new MainThreadExecutor();
}
/*
* returns the thread pool executor for background task
*/
public ThreadPoolExecutor forBackgroundTasks() {
return mForBackgroundTasks;
}
/*
* returns the thread pool executor for light weight background task
*/
public ThreadPoolExecutor forLightWeightBackgroundTasks() {
return mForLightWeightBackgroundTasks;
}
/*
* returns the thread pool executor for main thread task
*/
public Executor forMainThreadTasks() {
return mMainThreadExecutor;
}
}
注意:不同线程池可用的线程数取决于您的要求。
现在在您的代码中如下使用它
/*
* Using it for Background Tasks
*/
public void doSomeBackgroundWork(){
DefaultExecutorSupplier.getInstance().forBackgroundTasks()
.execute(new Runnable() {
@Override
public void run() {
// do some background work here.
}
});
}
/*
* Using it for Light-Weight Background Tasks
*/
public void doSomeLightWeightBackgroundWork(){
DefaultExecutorSupplier.getInstance().forLightWeightBackgroundTasks()
.execute(new Runnable() {
@Override
public void run() {
// do some light-weight background work here.
}
});
}
/*
* Using it for MainThread Tasks
*/
public void doSomeMainThreadWork(){
DefaultExecutorSupplier.getInstance().forMainThreadTasks()
.execute(new Runnable() {
@Override
public void run() {
// do some Main Thread work here.
}
});
}
这样,我们可以为网络任务、I/O任务、繁重的后台任务和其他任务创建不同的线程池。
如何取消任务?
要取消任务,您必须获得该任务的未来。因此,您需要使用提交,而不是使用执行,这将返回未来。现在,这个未来可以用来取消任务。
/*
* Get the future of the task by submitting it to the pool
*/
Future future = DefaultExecutorSupplier.getInstance().forBackgroundTasks()
.submit(new Runnable() {
@Override
public void run() {
// do some background work here.
}
});
/*
* cancelling the task
*/
future.cancel(true);
如何设置任务的优先级?
假设一个队列中有 20 个任务,而线程池仅容纳 4 个线程。我们根据优先级执行新任务,因为线程池一次只能执行 4 个。
但是假设我们需要首先执行我们推入队列的最后一个任务。我们需要为该任务设置 IMMEDIATE 优先级,以便当线程从队列中获取新任务时,它首先执行该任务(因为它具有最高优先级)。
要设置任务的优先级,我们需要创建一个线程池执行器。
为优先级创建一个 ENUM:
/**
* Priority levels
*/
public enum Priority {
/**
* NOTE: DO NOT CHANGE ORDERING OF THOSE CONSTANTS UNDER ANY CIRCUMSTANCES.
* Doing so will make ordering incorrect.
*/
/**
* Lowest priority level. Used for prefetches of data.
*/
LOW,
/**
* Medium priority level. Used for warming of data that might soon get visible.
*/
MEDIUM,
/**
* Highest priority level. Used for data that are currently visible on screen.
*/
HIGH,
/**
* Highest priority level. Used for data that are required instantly(mainly for emergency).
*/
IMMEDIATE;
}
创建一个 PriorityRunnable:
public class PriorityRunnable implements Runnable {
private final Priority priority;
public PriorityRunnable(Priority priority) {
this.priority = priority;
}
@Override
public void run() {
// nothing to do here.
}
public Priority getPriority() {
return priority;
}
}
创建一个 PriorityThreadPoolExecutor,它扩展了 ThreadPoolExecutor。我们必须创建 PriorityFutureTask,它将实现 Comparable<PriorityFutureTask>
public class PriorityThreadPoolExecutor extends ThreadPoolExecutor {
public PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
TimeUnit unit, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit,new PriorityBlockingQueue<Runnable>(), threadFactory);
}
@Override
public Future<?> submit(Runnable task) {
PriorityFutureTask futureTask = new PriorityFutureTask((PriorityRunnable) task);
execute(futureTask);
return futureTask;
}
private static final class PriorityFutureTask extends FutureTask<PriorityRunnable>
implements Comparable<PriorityFutureTask> {
private final PriorityRunnable priorityRunnable;
public PriorityFutureTask(PriorityRunnable priorityRunnable) {
super(priorityRunnable, null);
this.priorityRunnable = priorityRunnable;
}
/*
* compareTo() method is defined in interface java.lang.Comparable and it is used
* to implement natural sorting on java classes. natural sorting means the the sort
* order which naturally applies on object e.g. lexical order for String, numeric
* order for Integer or Sorting employee by there ID etc. most of the java core
* classes including String and Integer implements CompareTo() method and provide
* natural sorting.
*/
@Override
public int compareTo(PriorityFutureTask other) {
Priority p1 = priorityRunnable.getPriority();
Priority p2 = other.priorityRunnable.getPriority();
return p2.ordinal() - p1.ordinal();
}
}
}
首先,在 DefaultExecutorSupplier 中,使用 PriorityThreadPoolExecutor 而不是 ThreadPoolExecutor,如下所示:
public class DefaultExecutorSupplier{
private final PriorityThreadPoolExecutor mForBackgroundTasks;
private DefaultExecutorSupplier() {
mForBackgroundTasks = new PriorityThreadPoolExecutor(
NUMBER_OF_CORES * 2,
NUMBER_OF_CORES * 2,
60L,
TimeUnit.SECONDS,
backgroundPriorityThreadFactory
);
}
}
这是我们如何为任务设置高优先级的示例:
/*
* do some task at high priority
*/
public void doSomeTaskAtHighPriority(){
DefaultExecutorSupplier.getInstance().forBackgroundTasks()
.submit(new PriorityRunnable(Priority.HIGH) {
@Override
public void run() {
// do some background work here at high priority.
}
});
}
通过这种方式,可以确定任务的优先级