Browse Source
Previous AsyncTask would throw a RejectedExecutionException if too many AsyncTasks got spawned on the thread pool executor. The solution was to create a custom Executor that properly executed the task and queue it if necessary. Also switched to using weakreference for the view and set timeouts on image loading so it can load faster.master
Anthony Restaino
9 years ago
3 changed files with 90 additions and 32 deletions
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
package acr.browser.lightning.async; |
||||
|
||||
import android.support.annotation.NonNull; |
||||
import android.util.Log; |
||||
|
||||
import java.util.ArrayDeque; |
||||
import java.util.Queue; |
||||
import java.util.concurrent.Executor; |
||||
import java.util.concurrent.Executors; |
||||
import java.util.concurrent.RejectedExecutionException; |
||||
|
||||
/** |
||||
* Created 9/27/2015 Anthony Restaino |
||||
*/ |
||||
public class AsyncExecutor implements Executor { |
||||
|
||||
private static final String TAG = AsyncExecutor.class.getSimpleName(); |
||||
private static AsyncExecutor INSTANCE = new AsyncExecutor(); |
||||
private Queue<Runnable> mQueue = new ArrayDeque<>(1); |
||||
private Executor mExecutor = Executors.newFixedThreadPool(4); |
||||
|
||||
private AsyncExecutor() {} |
||||
|
||||
public static AsyncExecutor getInstance() { |
||||
return INSTANCE; |
||||
} |
||||
|
||||
public synchronized void notifyThreadFinish() { |
||||
if (mQueue.isEmpty()) { |
||||
return; |
||||
} |
||||
Runnable runnable = mQueue.remove(); |
||||
execute(runnable); |
||||
} |
||||
|
||||
@Override |
||||
protected void finalize() throws Throwable { |
||||
super.finalize(); |
||||
} |
||||
|
||||
@Override |
||||
public void execute(@NonNull Runnable command) { |
||||
try { |
||||
mExecutor.execute(command); |
||||
} catch (RejectedExecutionException ignored) { |
||||
mQueue.add(command); |
||||
Log.d(TAG, "Thread was enqueued"); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue