From 0e71e6cd066dad690ae433bdc1f279ff82cda871 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Fri, 30 Sep 2016 13:50:44 -0700 Subject: Make sure BackgroundTask can add threads when we have too many tasks --- .../github/daneren2005/dsub/util/BackgroundTask.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/src/main/java/github/daneren2005/dsub/util/BackgroundTask.java b/app/src/main/java/github/daneren2005/dsub/util/BackgroundTask.java index 31e83200..2b0c6279 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/BackgroundTask.java +++ b/app/src/main/java/github/daneren2005/dsub/util/BackgroundTask.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import org.xmlpull.v1.XmlPullParserException; @@ -54,6 +55,7 @@ public abstract class BackgroundTask implements ProgressListener { private static final Collection threads = Collections.synchronizedCollection(new ArrayList()); protected static final BlockingQueue queue = new LinkedBlockingQueue(10); private static Handler handler = null; + private static AtomicInteger currentlyRunning = new AtomicInteger(0); static { try { handler = new Handler(Looper.getMainLooper()); @@ -71,6 +73,11 @@ public abstract class BackgroundTask implements ProgressListener { threads.add(thread); thread.start(); } + } else if(currentlyRunning.get() >= threads.size()) { + Log.w(TAG, "Emergency add new thread: " + (threads.size() + 1)); + Thread thread = new Thread(new TaskRunnable(), String.format("BackgroundTask_%d", threads.size())); + threads.add(thread); + thread.start(); } if(handler == null) { try { @@ -304,22 +311,30 @@ public abstract class BackgroundTask implements ProgressListener { @Override public void run() { Looper.prepare(); + final Thread currentThread = Thread.currentThread(); while(running) { try { Task task = queue.take(); + currentlyRunning.incrementAndGet(); task.execute(); } catch(InterruptedException stop) { Log.e(TAG, "Thread died"); running = false; - threads.remove(Thread.currentThread()); } catch(Throwable t) { Log.e(TAG, "Unexpected crash in BackgroundTask thread", t); + running = false; } + + currentlyRunning.decrementAndGet(); + } + + if(threads.contains(currentThread)) { + threads.remove(currentThread); } } } - public static interface OnCancelListener { + public interface OnCancelListener { void onCancel(); } } -- cgit v1.2.3