aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2016-09-30 13:50:44 -0700
committerScott Jackson <daneren2005@gmail.com>2016-09-30 13:50:44 -0700
commit0e71e6cd066dad690ae433bdc1f279ff82cda871 (patch)
treee50468c791988ee16377f9b7640baecdf771e3d6 /app/src/main/java/github
parentbc2709aa309d4e62f35903bffb486b1eb1e5726f (diff)
downloaddsub-0e71e6cd066dad690ae433bdc1f279ff82cda871.tar.gz
dsub-0e71e6cd066dad690ae433bdc1f279ff82cda871.tar.bz2
dsub-0e71e6cd066dad690ae433bdc1f279ff82cda871.zip
Make sure BackgroundTask can add threads when we have too many tasks
Diffstat (limited to 'app/src/main/java/github')
-rw-r--r--app/src/main/java/github/daneren2005/dsub/util/BackgroundTask.java19
1 files changed, 17 insertions, 2 deletions
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<T> implements ProgressListener {
private static final Collection<Thread> threads = Collections.synchronizedCollection(new ArrayList<Thread>());
protected static final BlockingQueue<BackgroundTask.Task> queue = new LinkedBlockingQueue<BackgroundTask.Task>(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<T> 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<T> 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();
}
}