上一篇介绍了Glide的源码调用流程。Glide的源码使用了很多设计模式,也有非常多的类的设计是值得我们学习的。
Glide的线程管理都在GlideExecutor
这个类中。下面重点分析这个类的实现。
/**
* A prioritized {@link ThreadPoolExecutor} for running jobs in Glide.
*/
public final class GlideExecutor implements ExecutorService {
/**
* The default thread name prefix for executors used to load/decode/transform data not found in
* cache.
*/
private static final String DEFAULT_SOURCE_EXECUTOR_NAME = "source";
/**
* The default thread name prefix for executors used to load/decode/transform data found in
* Glide's cache.
*/
private static final String DEFAULT_DISK_CACHE_EXECUTOR_NAME = "disk-cache";
/**
* The default thread count for executors used to load/decode/transform data found in Glide's
* cache.
*/
private static final int DEFAULT_DISK_CACHE_EXECUTOR_THREADS = 1;//磁盘缓存线程池的默认线程数
private static final String TAG = "GlideExecutor";
/**
* The default thread name prefix for executors from unlimited thread pool used to
* load/decode/transform data not found in cache.
*/
private static final String SOURCE_UNLIMITED_EXECUTOR_NAME = "source-unlimited";
private static final String ANIMATION_EXECUTOR_NAME = "animation";
/**
* The default keep alive time for threads in our cached thread pools in milliseconds.
*/
private static final long KEEP_ALIVE_TIME_MS = TimeUnit.SECONDS.toMillis(10);
// Don't use more than four threads when automatically determining thread count..
private static final int MAXIMUM_AUTOMATIC_THREAD_COUNT = 4;//所有线程池最大的线程数,不超过4
// May be accessed on other threads, but this is an optimization only so it's ok if we set its
// value more than once.
private static volatile int bestThreadCount;//最佳线程数
private final ExecutorService delegate;
/**
* Returns a new fixed thread pool with the default thread count returned from
* {@link #calculateBestThreadCount()}, the {@link #DEFAULT_DISK_CACHE_EXECUTOR_NAME} thread name
* prefix, and the
* {@link com.bumptech.glide.load.engine.executor.GlideExecutor.UncaughtThrowableStrategy#DEFAULT}
* uncaught throwable strategy.未捕获异常的处理策略。默认是打印日志。
*
* <p>Disk cache executors do not allow network operations on their threads.
*/
public static GlideExecutor newDiskCacheExecutor() {
return newDiskCacheExecutor(
DEFAULT_DISK_CACHE_EXECUTOR_THREADS,
DEFAULT_DISK_CACHE_EXECUTOR_NAME,
UncaughtThrowableStrategy.DEFAULT);
}
/**
* Returns a new fixed thread pool with the default thread count returned from
* {@link #calculateBestThreadCount()}, the {@link #DEFAULT_DISK_CACHE_EXECUTOR_NAME} thread name
* prefix, and a custom
* {@link com.bumptech.glide.load.engine.executor.GlideExecutor.UncaughtThrowableStrategy}
* uncaught throwable strategy.
*
* <p>Disk cache executors do not allow network operations on their threads.
* @param uncaughtThrowableStrategy The {@link
* com.bumptech.glide.load.engine.executor.GlideExecutor.UncaughtThrowableStrategy} to use to
* handle uncaught exceptions.
*/
// Public API.
@SuppressWarnings("unused")
public static GlideExecutor newDiskCacheExecutor(
UncaughtThrowableStrategy uncaughtThrowableStrategy) {
return newDiskCacheExecutor(
DEFAULT_DISK_CACHE_EXECUTOR_THREADS,
DEFAULT_DISK_CACHE_EXECUTOR_NAME,
uncaughtThrowableStr