Using custom threadpool executor instead of the one defined in AsyncTask

Change-Id: I8fe95d932c5de14c74f0576bfc3d1f641d12b448
This commit is contained in:
Sunny Goyal
2015-09-23 15:38:09 -07:00
parent ce0a89461a
commit 8ac727b2c6
6 changed files with 21 additions and 19 deletions

View File

@@ -13,7 +13,6 @@ import android.os.AsyncTask;
import android.util.Log;
import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.compat.AppWidgetManagerCompat;
import java.util.ArrayList;
import java.util.List;
@@ -90,7 +89,7 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
}
LauncherAppState app = LauncherAppState.getInstanceNoCreate();

View File

@@ -94,7 +94,7 @@ public class DeleteDropTarget extends ButtonDropTarget {
appWidgetHost.deleteAppWidgetId(widget.appWidgetId);
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
}
} else {
return false;

View File

@@ -4735,7 +4735,7 @@ public class Launcher extends Activity
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
}
}
}

View File

@@ -61,6 +61,10 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -105,6 +109,18 @@ public final class Utilities {
public static final boolean ATLEAST_JB_MR2 =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
// 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;
/**
* An {@link Executor} to be used with async task with no limit on the queue size.
*/
public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
// To turn on these properties, type
// adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
private static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";

View File

@@ -43,25 +43,12 @@ 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<Runnable>());
private static final float WIDGET_PREVIEW_ICON_PADDING_PERCENTAGE = 0.25f;
private final HashMap<String, long[]> mPackageVersions = new HashMap<>();
@@ -109,7 +96,7 @@ public class WidgetPreviewLoader {
WidgetCacheKey key = getObjectKey(o, size);
PreviewLoadTask task = new PreviewLoadTask(key, o, previewWidth, previewHeight, caller);
task.executeOnExecutor(PREVIEW_LOAD_EXECUTOR);
task.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
return new PreviewLoadRequest(task);
}

View File

@@ -1326,7 +1326,7 @@ public class Workspace extends PagedView
mLauncher.overrideWallpaperDimensions());
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
}
protected void snapToPage(int whichPage, Runnable r) {