From 7ca991104e3ac2e99fcde5599416f82b4d66aad1 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Wed, 23 Sep 2015 10:16:38 -0700 Subject: [PATCH] Using an executor with increased queue size for loading previews An async task can be in the queue even if it has been cancelled. It is removed only after it gets executed once. So the executor can get blocked if there are some tasks which are running, and we keep posting and cancelling preview load tasks. Bug: 24306650 Change-Id: I836c8d53258542cc6f31952dff84323cc057437c --- .../android/launcher3/WidgetPreviewLoader.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/com/android/launcher3/WidgetPreviewLoader.java b/src/com/android/launcher3/WidgetPreviewLoader.java index 346055566c..e13d44c9b9 100644 --- a/src/com/android/launcher3/WidgetPreviewLoader.java +++ b/src/com/android/launcher3/WidgetPreviewLoader.java @@ -43,12 +43,25 @@ import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; public class WidgetPreviewLoader { private static final String TAG = "WidgetPreviewLoader"; private static final boolean DEBUG = false; + // These values are same as that in {@link AsyncTask}. + private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); + private static final int CORE_POOL_SIZE = CPU_COUNT + 1; + private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1; + private static final int KEEP_ALIVE = 1; + private static final Executor PREVIEW_LOAD_EXECUTOR = new ThreadPoolExecutor( + CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, + TimeUnit.SECONDS, new LinkedBlockingQueue()); + private static final float WIDGET_PREVIEW_ICON_PADDING_PERCENTAGE = 0.25f; private final HashMap mPackageVersions = new HashMap<>(); @@ -96,7 +109,7 @@ public class WidgetPreviewLoader { WidgetCacheKey key = getObjectKey(o, size); PreviewLoadTask task = new PreviewLoadTask(key, o, previewWidth, previewHeight, caller); - task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + task.executeOnExecutor(PREVIEW_LOAD_EXECUTOR); return new PreviewLoadRequest(task); }