summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorRomain Guy <romainguy@google.com>2013-08-09 16:09:44 -0700
committerRomain Guy <romainguy@google.com>2013-08-09 16:15:18 -0700
commit719c44e03b97e850a46136ba336d729f5fbd1f47 (patch)
treeca328754d89329cf072ad2dbf8e6c660251c82dc /core
parentd81a15c6b77c94109d0a08bc7355f62301fe9234 (diff)
downloadframeworks_base-719c44e03b97e850a46136ba336d729f5fbd1f47.zip
frameworks_base-719c44e03b97e850a46136ba336d729f5fbd1f47.tar.gz
frameworks_base-719c44e03b97e850a46136ba336d729f5fbd1f47.tar.bz2
Prevent AsyncTask from creating too many threads
Bug #10228005 From the ThreadPoolExecutor documentation: - If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing. - If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread. - If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected. Before this change AsyncTask could create up to 128 threads because of the limited queue of 10 items (the capacity of a blocking queue is fixed.) This change increases the size of the queue to 128 items and reduces the maximum number of threads to the number of CPU cores * 2 + 1. Apps can still submit the same number of tasks. Change-Id: I015d77b53b6a9fda39c618830b34d45a10de5571
Diffstat (limited to 'core')
-rw-r--r--core/java/android/os/AsyncTask.java7
1 files changed, 4 insertions, 3 deletions
diff --git a/core/java/android/os/AsyncTask.java b/core/java/android/os/AsyncTask.java
index ce5f163..d4a3006 100644
--- a/core/java/android/os/AsyncTask.java
+++ b/core/java/android/os/AsyncTask.java
@@ -177,8 +177,9 @@ import java.util.concurrent.atomic.AtomicInteger;
public abstract class AsyncTask<Params, Progress, Result> {
private static final String LOG_TAG = "AsyncTask";
- private static final int CORE_POOL_SIZE = 5;
- private static final int MAXIMUM_POOL_SIZE = 128;
+ 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 ThreadFactory sThreadFactory = new ThreadFactory() {
@@ -190,7 +191,7 @@ public abstract class AsyncTask<Params, Progress, Result> {
};
private static final BlockingQueue<Runnable> sPoolWorkQueue =
- new LinkedBlockingQueue<Runnable>(10);
+ new LinkedBlockingQueue<Runnable>(128);
/**
* An {@link Executor} that can be used to execute tasks in parallel.