From 227c8d78bf3a31328a828ee128f0dbf0d206e90a Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 11 Feb 2014 16:36:08 -0800 Subject: #167 Move all the task handlers to a common thread pool --- .../daneren2005/dsub/util/BackgroundTask.java | 96 ++++++++++++++++++++-- src/github/daneren2005/dsub/util/LoadingTask.java | 51 ++++-------- .../dsub/util/SilentBackgroundTask.java | 26 +----- .../daneren2005/dsub/util/TabBackgroundTask.java | 46 ++++------- 4 files changed, 121 insertions(+), 98 deletions(-) (limited to 'src/github/daneren2005') 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 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 threads = Collections.synchronizedCollection(new ArrayList(DEFAULT_CONCURRENCY)); + private static final BlockingQueue queue = new BlockingQueue(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 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 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 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 extends BackgroundTask { 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 extends BackgroundTask { } } - 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 extends BackgroundTask { - 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 extends BackgroundTask { 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(); } -- cgit v1.2.3