aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java
diff options
context:
space:
mode:
authorDan Zivkovic <zivkovic@google.com>2015-03-12 17:06:48 -0700
committerDan Zivkovic <zivkovic@google.com>2015-03-12 17:10:38 -0700
commiteaa710d4aaac75ff2b7e29608d004fe7662b392e (patch)
tree6944df334be797522526b5e3f83484933e0b034d /java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java
parent26fb83c481034cb9dbc9504e60fde40c6b213e97 (diff)
downloadlatinime-eaa710d4aaac75ff2b7e29608d004fe7662b392e.tar.gz
latinime-eaa710d4aaac75ff2b7e29608d004fe7662b392e.tar.xz
latinime-eaa710d4aaac75ff2b7e29608d004fe7662b392e.zip
Separate executor for the Spelling decoder.
Bug 19710676. Change-Id: I6e66eddd507c11e424105869833fe6841b90275d
Diffstat (limited to 'java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java')
-rw-r--r--java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java57
1 files changed, 45 insertions, 12 deletions
diff --git a/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java b/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java
index a78446103..8ce6eff92 100644
--- a/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java
@@ -33,17 +33,30 @@ public class ExecutorUtils {
private static final String TAG = "ExecutorUtils";
- private static ScheduledExecutorService sExecutorService =
- Executors.newSingleThreadScheduledExecutor(new ExecutorFactory());
+ public static final String KEYBOARD = "Keyboard";
+ public static final String SPELLING = "Spelling";
+
+ private static ScheduledExecutorService sKeyboardExecutorService = newExecutorService(KEYBOARD);
+ private static ScheduledExecutorService sSpellingExecutorService = newExecutorService(SPELLING);
+
+ private static ScheduledExecutorService newExecutorService(final String name) {
+ return Executors.newSingleThreadScheduledExecutor(new ExecutorFactory(name));
+ }
private static class ExecutorFactory implements ThreadFactory {
+ private final String mName;
+
+ private ExecutorFactory(final String name) {
+ mName = name;
+ }
+
@Override
public Thread newThread(final Runnable runnable) {
Thread thread = new Thread(runnable, TAG);
thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
- Log.w(TAG + "-" + runnable.getClass().getSimpleName(), ex);
+ Log.w(mName + "-" + runnable.getClass().getSimpleName(), ex);
}
});
return thread;
@@ -64,24 +77,44 @@ public class ExecutorUtils {
//
/**
+ * @param name Executor's name.
* @return scheduled executor service used to run background tasks
*/
- public static ScheduledExecutorService getBackgroundExecutor() {
+ public static ScheduledExecutorService getBackgroundExecutor(final String name) {
if (sExecutorServiceForTests != null) {
return sExecutorServiceForTests;
}
- return sExecutorService;
+ switch (name) {
+ case KEYBOARD:
+ return sKeyboardExecutorService;
+ case SPELLING:
+ return sSpellingExecutorService;
+ default:
+ throw new IllegalArgumentException("Invalid executor: " + name);
+ }
}
- public static void killTasks() {
- getBackgroundExecutor().shutdownNow();
+ public static void killTasks(final String name) {
+ final ScheduledExecutorService executorService = getBackgroundExecutor(name);
+ executorService.shutdownNow();
try {
- getBackgroundExecutor().awaitTermination(5, TimeUnit.SECONDS);
+ executorService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
- Log.wtf(TAG, "Failed to shut down background task.");
- throw new IllegalStateException("Failed to shut down background task.");
- } finally {
- sExecutorService = Executors.newSingleThreadScheduledExecutor(new ExecutorFactory());
+ Log.wtf(TAG, "Failed to shut down: " + name);
+ }
+ if (executorService == sExecutorServiceForTests) {
+ // Don't do anything to the test service.
+ return;
+ }
+ switch (name) {
+ case KEYBOARD:
+ sKeyboardExecutorService = newExecutorService(KEYBOARD);
+ break;
+ case SPELLING:
+ sSpellingExecutorService = newExecutorService(SPELLING);
+ break;
+ default:
+ throw new IllegalArgumentException("Invalid executor: " + name);
}
}