我正在迁移一个应用程序以从v3滑行到v4.7.1,并注意到已删除FifoPriorityThreadPoolExecutor类。我们使用它来限制Glide用于执行网络操作的线程数。
旧代码
public class GlideConfiguration implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setResizeService(new FifoPriorityThreadPoolExecutor(1));
}
...
}
新代码
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
private static final String FIFO_SOURCE_EXECUTOR_NAME = "fifo-source";
@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
builder.setSourceExecutor(newSourceExecutor(1, FIFO_SOURCE_EXECUTOR_NAME, GlideExecutor.UncaughtThrowableStrategy.DEFAULT));
}
...
}
但是它仍然使用4个线程。我在做什么错了?
答案 0 :(得分:1)
检查glide的源代码之后。 仅当未向构建器提供源执行器时,Glide才会初始化其默认源执行器。
Glide build(@NonNull Context context) {
if (sourceExecutor == null) {
sourceExecutor = GlideExecutor.newSourceExecutor();
}
if (diskCacheExecutor == null) {
diskCacheExecutor = GlideExecutor.newDiskCacheExecutor();
}
if (animationExecutor == null) {
animationExecutor = GlideExecutor.newAnimationExecutor();
}
...
}
// Glide executor is simply a wrapper over ThreadPoolExecutor class.
public static GlideExecutor newSourceExecutor(int threadCount, String name, UncaughtThrowableStrategy uncaughtThrowableStrategy) {
return new GlideExecutor(new ThreadPoolExecutor(threadCount /* corePoolSize */,threadCount /* maximumPoolSize */,0 /* keepAliveTime */,TimeUnit.MILLISECONDS,new PriorityBlockingQueue<Runnable>(),new DefaultThreadFactory(name, uncaughtThrowableStrategy, false)));
}
由于您正在使用Stetho库检查线程,以监视不显示有关线程的任何信息的网络调用。这些请求很可能是由单线程发起的。
要更好地了解应用程序中的线程使用情况,应使用Android monitor -> DDMS -> Threads.