aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2014-02-11 16:36:08 -0800
committerScott Jackson <daneren2005@gmail.com>2014-02-11 16:36:08 -0800
commit227c8d78bf3a31328a828ee128f0dbf0d206e90a (patch)
tree16eae010753948cbc9587816670e297125030539
parente4b23a8076f1b420b439744fc05b646f3ce19c1a (diff)
downloaddsub-227c8d78bf3a31328a828ee128f0dbf0d206e90a.tar.gz
dsub-227c8d78bf3a31328a828ee128f0dbf0d206e90a.tar.bz2
dsub-227c8d78bf3a31328a828ee128f0dbf0d206e90a.zip
#167 Move all the task handlers to a common thread pool
-rw-r--r--src/github/daneren2005/dsub/util/BackgroundTask.java96
-rw-r--r--src/github/daneren2005/dsub/util/LoadingTask.java51
-rw-r--r--src/github/daneren2005/dsub/util/SilentBackgroundTask.java26
-rw-r--r--src/github/daneren2005/dsub/util/TabBackgroundTask.java46
4 files changed, 121 insertions, 98 deletions
diff --git a/src/github/daneren2005/dsub/util/BackgroundTask.java b/src/github/daneren2005/dsub/util/BackgroundTask.java
index 547bbd1e..4ff1f20a 100644
--- a/src/github/daneren2005/dsub/util/BackgroundTask.java
+++ b/src/github/daneren2005/dsub/util/BackgroundTask.java
@@ -33,16 +33,35 @@ import github.daneren2005.dsub.view.ErrorDialog;
* @author Sindre Mehus
*/
public abstract class BackgroundTask<T> implements ProgressListener {
-
private static final String TAG = BackgroundTask.class.getSimpleName();
+
private final Activity activity;
- private final Handler handler;
+
+ private static final DEFAULT_CONCURRENCY = 5;
+ private static final Collection<Thread> threads = Collections.synchronizedCollection(new ArrayList<Thread>(DEFAULT_CONCURRENCY));
+ private static final BlockingQueue<Task> queue = new BlockingQueue<Task>(10);
+ private static final Handler handler = new Handler();
public BackgroundTask(Activity activity) {
this.activity = activity;
- handler = new Handler();
+
+ if(threads.isEmpty()) {
+ for(int i = 0; i < DEFAULT_CONCURRENY; i++) {
+ Thread thread = new Thread(new TaskRunnable(), String.format("BackgroundTask_%d", i));
+ threads.add(thread);
+ thread.start();
+ }
+ }
}
+ public static void stopThreads() {
+ for(Thread thread: threads) {
+ thread.interrupt();
+ }
+ threads.clear();
+ queue.clear();
+ }
+
protected Activity getActivity() {
return activity;
}
@@ -51,7 +70,7 @@ public abstract class BackgroundTask<T> implements ProgressListener {
return handler;
}
- public abstract void execute();
+ public abstract void execute();
protected abstract T doInBackground() throws Throwable;
@@ -87,6 +106,10 @@ public abstract class BackgroundTask<T> implements ProgressListener {
return error.getClass().getSimpleName();
}
+ protected boolean isCancelled() {
+ return false;
+ }
+
@Override
public abstract void updateProgress(final String message);
@@ -94,4 +117,67 @@ public abstract class BackgroundTask<T> implements ProgressListener {
public void updateProgress(int messageId) {
updateProgress(activity.getResources().getString(messageId));
}
-} \ No newline at end of file
+
+ protected class Task {
+ private void execute() {
+ try {
+ final T result = doInBackground();
+ if(isCancelled()) {
+ return;
+ }
+
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+ onDone(result);
+ }
+ });
+ } catch(Throwable t) {
+ if(isCancelled()) {
+ return;
+ }
+
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ onError(t);
+ } catch(Exception e) {
+ // Don't care
+ }
+ }
+ });
+ }
+ }
+
+ public void onDone(T result) {
+ done(result);
+ }
+ public void onError(Throwable t) {
+ error(t);
+ }
+ }
+
+ private class TaskRunnable extends Runnable {
+ private boolean running = true;
+
+ public TaskRunnable() {
+
+ }
+
+ @Override
+ public void run() {
+ while(running) {
+ try {
+ Task task = queue.take();
+ task.execute();
+ } catch(InterruptedException stop) {
+ running = false;
+ } catch(Throwable t) {
+ Log.e(TAG, "Unexpected crash in BackgroundTask thread", t);
+ }
+ }
+ }
+ }
+
+}
diff --git a/src/github/daneren2005/dsub/util/LoadingTask.java b/src/github/daneren2005/dsub/util/LoadingTask.java
index 7faa6afe..55d67dc9 100644
--- a/src/github/daneren2005/dsub/util/LoadingTask.java
+++ b/src/github/daneren2005/dsub/util/LoadingTask.java
@@ -35,45 +35,21 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> {
public void onCancel(DialogInterface dialog) {
cancel();
}
-
});
- thread = new Thread() {
- @Override
- public void run() {
- try {
- final T result = doInBackground();
- if (isCancelled()) {
- return;
- }
+ queue.offer(new Task() {
+ @Override
+ public void onDone(T result) {
+ loading.cancel();
+ done(result);
+ }
- getHandler().post(new Runnable() {
- @Override
- public void run() {
- loading.cancel();
- done(result);
- }
- });
- } catch (final Throwable t) {
- if (isCancelled()) {
- return;
- }
-
- getHandler().post(new Runnable() {
- @Override
- public void run() {
- try {
- loading.cancel();
- error(t);
- } catch(Exception e) {
- // Don't care
- }
- }
- });
- }
- }
- };
- thread.start();
+ @Override
+ public void onError(Throwable t) {
+ loading.cancel();
+ error(t);
+ }
+ });
}
protected void cancel() {
@@ -83,7 +59,8 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> {
}
}
- private boolean isCancelled() {
+ @Override
+ protected boolean isCancelled() {
return (tabActivity instanceof SubsonicActivity && ((SubsonicActivity)tabActivity).isDestroyed()) || cancelled;
}
diff --git a/src/github/daneren2005/dsub/util/SilentBackgroundTask.java b/src/github/daneren2005/dsub/util/SilentBackgroundTask.java
index 7bceb467..f361954a 100644
--- a/src/github/daneren2005/dsub/util/SilentBackgroundTask.java
+++ b/src/github/daneren2005/dsub/util/SilentBackgroundTask.java
@@ -24,37 +24,13 @@ import android.app.Activity;
* @author Sindre Mehus
*/
public abstract class SilentBackgroundTask<T> extends BackgroundTask<T> {
-
public SilentBackgroundTask(Activity activity) {
super(activity);
}
@Override
public void execute() {
- Thread thread = new Thread() {
- @Override
- public void run() {
- try {
- final T result = doInBackground();
-
- getHandler().post(new Runnable() {
- @Override
- public void run() {
- done(result);
- }
- });
-
- } catch (final Throwable t) {
- getHandler().post(new Runnable() {
- @Override
- public void run() {
- error(t);
- }
- });
- }
- }
- };
- thread.start();
+ queue.offer(new Task());
}
@Override
diff --git a/src/github/daneren2005/dsub/util/TabBackgroundTask.java b/src/github/daneren2005/dsub/util/TabBackgroundTask.java
index c345b982..8a243d8e 100644
--- a/src/github/daneren2005/dsub/util/TabBackgroundTask.java
+++ b/src/github/daneren2005/dsub/util/TabBackgroundTask.java
@@ -19,39 +19,23 @@ public abstract class TabBackgroundTask<T> extends BackgroundTask<T> {
public void execute() {
tabFragment.setProgressVisible(true);
- new Thread() {
- @Override
- public void run() {
- try {
- final T result = doInBackground();
- if (isCancelled()) {
- return;
- }
-
- getHandler().post(new Runnable() {
- @Override
- public void run() {
- tabFragment.setProgressVisible(false);
- done(result);
- }
- });
- } catch (final Throwable t) {
- if (isCancelled()) {
- return;
- }
- getHandler().post(new Runnable() {
- @Override
- public void run() {
- tabFragment.setProgressVisible(false);
- error(t);
- }
- });
- }
- }
- }.start();
+ queue.offer(new Task() {
+ @Override
+ public void onDone(T result) {
+ tabFragment.setProgressVisible(false);
+ done(result);
+ }
+
+ @Override
+ public void onError(Throwable t) {
+ tabFragment.setProgressVisible(false);
+ error(t);
+ }
+ });
}
- private boolean isCancelled() {
+ @Override
+ protected boolean isCancelled() {
return !tabFragment.isAdded();
}