Niedzielski has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/289332

Change subject: Reimplement AsyncTask thread pool in SaneAsyncTask
......................................................................

Reimplement AsyncTask thread pool in SaneAsyncTask

Copy the AsyncTask AOSP implementation to prevent low OEM
implementations. Test failure scenario by setting CPU_COUNT and
QUEUE_SIZE to 1.

Bug: T135572
Change-Id: I2ad6804a776a69d3a1dd2674a7ff8fbfcba71186
---
M app/src/main/java/org/wikipedia/concurrency/SaneAsyncTask.java
1 file changed, 30 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/32/289332/1

diff --git a/app/src/main/java/org/wikipedia/concurrency/SaneAsyncTask.java 
b/app/src/main/java/org/wikipedia/concurrency/SaneAsyncTask.java
index 0d0216a..7a1a396 100644
--- a/app/src/main/java/org/wikipedia/concurrency/SaneAsyncTask.java
+++ b/app/src/main/java/org/wikipedia/concurrency/SaneAsyncTask.java
@@ -1,11 +1,39 @@
 package org.wikipedia.concurrency;
 
 import android.os.AsyncTask;
+import android.support.annotation.NonNull;
 
 import org.wikipedia.util.log.L;
 
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Executor;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
 public abstract class SaneAsyncTask<T> extends AsyncTask<Void, Void, T> {
     private Throwable thrown;
+
+    // Reimplement the stock AOSP AsyncTask implementation which may differ on 
OEM devices.
+    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 QUEUE_SIZE = 128;
+    private static final int KEEP_ALIVE = 1;
+
+    private static final ThreadFactory THREAD_FACTORY = new ThreadFactory() {
+        private final AtomicInteger count = new AtomicInteger(1);
+        @Override public Thread newThread(@NonNull Runnable runnable) {
+            return new Thread(runnable, "AsyncTask #" + 
count.getAndIncrement());
+        }
+    };
+
+    private static final BlockingQueue<Runnable> POOL_WORK_QUEUE = new 
LinkedBlockingQueue<>(QUEUE_SIZE);
+
+    private static final Executor THREAD_POOL_EXECUTOR = new 
ThreadPoolExecutor(CORE_POOL_SIZE,
+            MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, POOL_WORK_QUEUE, 
THREAD_FACTORY);
 
     public abstract T performTask() throws Throwable;
 
@@ -16,7 +44,7 @@
     public void onCatch(Throwable caught) { }
 
     public void execute() {
-        super.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
+        super.executeOnExecutor(THREAD_POOL_EXECUTOR);
     }
 
     public void cancel() {
@@ -57,4 +85,4 @@
         super.onPreExecute();
         onBeforeExecute();
     }
-}
\ No newline at end of file
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/289332
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2ad6804a776a69d3a1dd2674a7ff8fbfcba71186
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to