Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9e2d271 authored by Dan Zivkovic's avatar Dan Zivkovic
Browse files

Use a single background executor.

Bug 19625976.

Change-Id: Ia03f440a31b059b5af42d162e1145330bf7b5ddf
parent 2151095b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ public class ContactsContentObserver implements Runnable {
        mContentObserver = new ContentObserver(null /* handler */) {
            @Override
            public void onChange(boolean self) {
                ExecutorUtils.getExecutorForDynamicLanguageModelUpdate()
                ExecutorUtils.getBackgroundExecutor()
                        .execute(ContactsContentObserver.this);
            }
        };
+1 −1
Original line number Diff line number Diff line
@@ -444,7 +444,7 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
            final Locale[] locales, final DictionaryInitializationListener listener) {
        final CountDownLatch latchForWaitingLoadingMainDictionary = new CountDownLatch(1);
        mLatchForWaitingLoadingMainDictionaries = latchForWaitingLoadingMainDictionary;
        ExecutorUtils.getExecutorForStaticLanguageModelUpdate().execute(new Runnable() {
        ExecutorUtils.getBackgroundExecutor().execute(new Runnable() {
            @Override
            public void run() {
                doReloadUninitializedMainDictionaries(
+1 −1
Original line number Diff line number Diff line
@@ -168,7 +168,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
    }

    private static void asyncExecuteTaskWithLock(final Lock lock, final Runnable task) {
        ExecutorUtils.getExecutorForDynamicLanguageModelUpdate().execute(new Runnable() {
        ExecutorUtils.getBackgroundExecutor().execute(new Runnable() {
            @Override
            public void run() {
                lock.lock();
+2 −2
Original line number Diff line number Diff line
@@ -143,7 +143,7 @@ public class UserDictionaryLookup implements Closeable {
            }

            // Schedule a new reload after RELOAD_DELAY_MS.
            mReloadFuture = ExecutorUtils.getExecutorForDynamicLanguageModelUpdate().schedule(
            mReloadFuture = ExecutorUtils.getBackgroundExecutor().schedule(
                    mLoader, RELOAD_DELAY_MS, TimeUnit.MILLISECONDS);
        }
    }
@@ -186,7 +186,7 @@ public class UserDictionaryLookup implements Closeable {
        // Schedule the initial load to run immediately.  It's possible that the first call to
        // isValidWord occurs before the dictionary has actually loaded, so it should not
        // assume that the dictionary has been loaded.
        ExecutorUtils.getExecutorForDynamicLanguageModelUpdate().execute(mLoader);
        ExecutorUtils.getBackgroundExecutor().execute(mLoader);

        // Register the observer to be notified on changes to the UserDictionary and all individual
        // items.
+9 −46
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
@@ -31,11 +30,10 @@ import java.util.concurrent.ThreadFactory;
 */
public class ExecutorUtils {

    private static final String STATIC_LANGUAGE_MODEL_UPDATE = "StaticLanguageModelUpdate";
    private static final String DYNAMIC_LANGUAGE_MODEL_UPDATE = "DynamicLanguageModelUpdate";
    private static final String TAG = "ExecutorUtils";

    private static final ConcurrentHashMap<String, ScheduledExecutorService> sExecutorMap =
            new ConcurrentHashMap<>();
    private static final ScheduledExecutorService sExecutorService =
            Executors.newSingleThreadScheduledExecutor(new ExecutorFactory());

    @UsedForTesting
    private static ScheduledExecutorService sExecutorServiceForTests;
@@ -47,37 +45,13 @@ public class ExecutorUtils {
    }

    /**
     * @return scheduled executor service used to update static language models
     * @return scheduled executor service used to run background tasks
     */
    public static ScheduledExecutorService getExecutorForStaticLanguageModelUpdate() {
        return getExecutor(STATIC_LANGUAGE_MODEL_UPDATE);
    }

    /**
     * @return scheduled executor service used to update dynamic language models
     */
    public static ScheduledExecutorService getExecutorForDynamicLanguageModelUpdate() {
        return getExecutor(DYNAMIC_LANGUAGE_MODEL_UPDATE);
    }

    /**
     * Gets the executor for the given id.
     */
    private static ScheduledExecutorService getExecutor(final String id) {
    public static ScheduledExecutorService getBackgroundExecutor() {
        if (sExecutorServiceForTests != null) {
            return sExecutorServiceForTests;
        }
        ScheduledExecutorService executor = sExecutorMap.get(id);
        if (executor == null) {
            synchronized (sExecutorMap) {
                executor = sExecutorMap.get(id);
                if (executor == null) {
                    executor = Executors.newSingleThreadScheduledExecutor(new ExecutorFactory(id));
                    sExecutorMap.put(id, executor);
                }
            }
        }
        return executor;
        return sExecutorService;
    }

    /**
@@ -85,28 +59,17 @@ public class ExecutorUtils {
     */
    @UsedForTesting
    public static void shutdownAllExecutors() {
        synchronized (sExecutorMap) {
            for (final ScheduledExecutorService executor : sExecutorMap.values()) {
                executor.execute(new ExecutorShutdown(executor));
            }
            sExecutorMap.clear();
        }
        sExecutorService.execute(new ExecutorShutdown(sExecutorService));
    }

    private static class ExecutorFactory implements ThreadFactory {
        private final String mThreadName;

        public ExecutorFactory(final String threadName) {
            mThreadName = threadName;
        }

        @Override
        public Thread newThread(final Runnable runnable) {
            Thread thread = new Thread(runnable, mThreadName);
            Thread thread = new Thread(runnable, TAG);
            thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread thread, Throwable ex) {
                    Log.w(mThreadName + "-" + runnable.getClass().getSimpleName(), ex);
                    Log.w(TAG + "-" + runnable.getClass().getSimpleName(), ex);
                }
            });
            return thread;
Loading