am 6f354b55: am 61024028: am a49bf128: am 33fce975: Merge "[Refactor] Divide BinaryDictInputOutput into BinaryDictEncoder and BinaryDictDecoder."

* commit '6f354b55053ee2a270e122e9c871a7ed6e9d9694':
diff --git a/java/res/values-et-rEE/strings-appname.xml b/java/res/values-et-rEE/strings-appname.xml
deleted file mode 100644
index bbc13d2..0000000
--- a/java/res/values-et-rEE/strings-appname.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- 
-/*
-**
-** Copyright 2013, The Android Open Source Project
-**
-** Licensed under the Apache License, Version 2.0 (the "License");
-** you may not use this file except in compliance with the License.
-** You may obtain a copy of the License at
-**
-**     http://www.apache.org/licenses/LICENSE-2.0
-**
-** Unless required by applicable law or agreed to in writing, software
-** distributed under the License is distributed on an "AS IS" BASIS,
-** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-** See the License for the specific language governing permissions and
-** limitations under the License.
-*/
- -->
-
-<resources xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <string name="english_ime_name" msgid="5940510615957428904">"Androidi klaviatuur (AOSP)"</string>
-    <string name="spell_checker_service_name" msgid="1254221805440242662">"Androidi õigekirjakontroll (AOSP)"</string>
-    <string name="english_ime_settings" msgid="5760361067176802794">"Androidi klaviatuuri seaded (AOSP)"</string>
-    <string name="android_spell_checker_settings" msgid="6123949487832861885">"Androidi õigekirjakontrolli seaded (AOSP)"</string>
-</resources>
diff --git a/java/res/values-ka-rGE/strings-appname.xml b/java/res/values-ka-rGE/strings-appname.xml
deleted file mode 100644
index 703c66a..0000000
--- a/java/res/values-ka-rGE/strings-appname.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- 
-/*
-**
-** Copyright 2013, The Android Open Source Project
-**
-** Licensed under the Apache License, Version 2.0 (the "License");
-** you may not use this file except in compliance with the License.
-** You may obtain a copy of the License at
-**
-**     http://www.apache.org/licenses/LICENSE-2.0
-**
-** Unless required by applicable law or agreed to in writing, software
-** distributed under the License is distributed on an "AS IS" BASIS,
-** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-** See the License for the specific language governing permissions and
-** limitations under the License.
-*/
- -->
-
-<resources xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <string name="english_ime_name" msgid="5940510615957428904">"Android-ის კლავიატურა (AOSP)"</string>
-    <string name="spell_checker_service_name" msgid="1254221805440242662">"Android-ის მართლწერის შემმოწმებელი (AOSP)"</string>
-    <string name="english_ime_settings" msgid="5760361067176802794">"Android-ის კლავიატურის პარამეტრები (AOSP)"</string>
-    <string name="android_spell_checker_settings" msgid="6123949487832861885">"Android-ის მართლწერის შემმოწმებლის პარამეტრები (AOSP)"</string>
-</resources>
diff --git a/java/res/values-kk/strings-appname.xml b/java/res/values-kk/strings-appname.xml
deleted file mode 100644
index 1e201fa..0000000
--- a/java/res/values-kk/strings-appname.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- 
-/*
-**
-** Copyright 2013, The Android Open Source Project
-**
-** Licensed under the Apache License, Version 2.0 (the "License");
-** you may not use this file except in compliance with the License.
-** You may obtain a copy of the License at
-**
-**     http://www.apache.org/licenses/LICENSE-2.0
-**
-** Unless required by applicable law or agreed to in writing, software
-** distributed under the License is distributed on an "AS IS" BASIS,
-** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-** See the License for the specific language governing permissions and
-** limitations under the License.
-*/
- -->
-
-<resources xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <string name="english_ime_name" msgid="5940510615957428904">"Android пернетақтасы (AOSP)"</string>
-    <string name="spell_checker_service_name" msgid="1254221805440242662">"Android емлені тексеру құралы (AOSP)"</string>
-    <string name="english_ime_settings" msgid="5760361067176802794">"Android пернетақтасының параметрлері (AOSP)"</string>
-    <string name="android_spell_checker_settings" msgid="6123949487832861885">"Android емлені тексеру құралының параметрлері (AOSP)"</string>
-</resources>
diff --git a/java/res/values-mn-rMN/strings-appname.xml b/java/res/values-mn-rMN/strings-appname.xml
deleted file mode 100644
index 6c27e3d..0000000
--- a/java/res/values-mn-rMN/strings-appname.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- 
-/*
-**
-** Copyright 2013, The Android Open Source Project
-**
-** Licensed under the Apache License, Version 2.0 (the "License");
-** you may not use this file except in compliance with the License.
-** You may obtain a copy of the License at
-**
-**     http://www.apache.org/licenses/LICENSE-2.0
-**
-** Unless required by applicable law or agreed to in writing, software
-** distributed under the License is distributed on an "AS IS" BASIS,
-** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-** See the License for the specific language governing permissions and
-** limitations under the License.
-*/
- -->
-
-<resources xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <string name="english_ime_name" msgid="5940510615957428904">"Андройд Гар (AOSP)"</string>
-    <string name="spell_checker_service_name" msgid="1254221805440242662">"Андройд Алдаа Шалгагч (AOSP)"</string>
-    <string name="english_ime_settings" msgid="5760361067176802794">"Андройд Гарын Тохиргоо (AOSP)"</string>
-    <string name="android_spell_checker_settings" msgid="6123949487832861885">"Андройд Алдаа Шалгагчийн Тохиргоо (AOSP)"</string>
-</resources>
diff --git a/java/res/values-ms-rMY/strings-appname.xml b/java/res/values-ms-rMY/strings-appname.xml
deleted file mode 100644
index 76d1d29..0000000
--- a/java/res/values-ms-rMY/strings-appname.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- 
-/*
-**
-** Copyright 2013, The Android Open Source Project
-**
-** Licensed under the Apache License, Version 2.0 (the "License");
-** you may not use this file except in compliance with the License.
-** You may obtain a copy of the License at
-**
-**     http://www.apache.org/licenses/LICENSE-2.0
-**
-** Unless required by applicable law or agreed to in writing, software
-** distributed under the License is distributed on an "AS IS" BASIS,
-** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-** See the License for the specific language governing permissions and
-** limitations under the License.
-*/
- -->
-
-<resources xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <string name="english_ime_name" msgid="5940510615957428904">"Papan Kekunci Android (AOSP)"</string>
-    <string name="spell_checker_service_name" msgid="1254221805440242662">"Penyemak Ejaan Android (AOSP)"</string>
-    <string name="english_ime_settings" msgid="5760361067176802794">"Tetapan Papan Kekunci Android (AOSP)"</string>
-    <string name="android_spell_checker_settings" msgid="6123949487832861885">"Tetapan Penyemak Ejaan Android (AOSP)"</string>
-</resources>
diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
index 99859de..dc4b0a0 100644
--- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
@@ -34,6 +34,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
@@ -69,14 +70,15 @@
             FormatSpec.FileHeader.ATTRIBUTE_VALUE_TRUE;
 
     /**
-     * A static map of time recorders, each of which records the time of accesses to a single binary
-     * dictionary file. The key for this map is the filename and the value is the shared dictionary
-     * time recorder associated with that filename.
+     * A static map of update controllers, each of which records the time of accesses to a single
+     * binary dictionary file and tracks whether the file is regenerating. The key for this map is
+     * the filename and the value is the shared dictionary time recorder associated with that
+     * filename.
      */
-    private static volatile ConcurrentHashMap<String, DictionaryTimeRecorder>
-            sFilenameDictionaryTimeRecorderMap = CollectionUtils.newConcurrentHashMap();
+    private static final ConcurrentHashMap<String, DictionaryUpdateController>
+            sFilenameDictionaryUpdateControllerMap = CollectionUtils.newConcurrentHashMap();
 
-    private static volatile ConcurrentHashMap<String, PrioritizedSerialExecutor>
+    private static final ConcurrentHashMap<String, PrioritizedSerialExecutor>
             sFilenameExecutorMap = CollectionUtils.newConcurrentHashMap();
 
     /** The application context. */
@@ -103,13 +105,13 @@
     private final boolean mIsUpdatable;
 
     // TODO: remove, once dynamic operations is serialized
-    /** Records access to the shared binary dictionary file across multiple instances. */
-    private final DictionaryTimeRecorder mFilenameDictionaryTimeRecorder;
+    /** Controls updating the shared binary dictionary file across multiple instances. */
+    private final DictionaryUpdateController mFilenameDictionaryUpdateController;
 
     // TODO: remove, once dynamic operations is serialized
-    /** Records access to the local binary dictionary for this instance. */
-    private final DictionaryTimeRecorder mPerInstanceDictionaryTimeRecorder =
-            new DictionaryTimeRecorder();
+    /** Controls updating the local binary dictionary for this instance. */
+    private final DictionaryUpdateController mPerInstanceDictionaryUpdateController =
+            new DictionaryUpdateController();
 
     /* A extension for a binary dictionary file. */
     public static final String DICT_FILE_EXTENSION = ".dict";
@@ -131,15 +133,15 @@
     protected abstract boolean hasContentChanged();
 
     /**
-     * Gets the dictionary time recorder for the given filename.
+     * Gets the dictionary update controller for the given filename.
      */
-    private static DictionaryTimeRecorder getDictionaryTimeRecorder(
+    private static DictionaryUpdateController getDictionaryUpdateController(
             String filename) {
-        DictionaryTimeRecorder recorder = sFilenameDictionaryTimeRecorderMap.get(filename);
+        DictionaryUpdateController recorder = sFilenameDictionaryUpdateControllerMap.get(filename);
         if (recorder == null) {
-            synchronized(sFilenameDictionaryTimeRecorderMap) {
-                recorder = new DictionaryTimeRecorder();
-                sFilenameDictionaryTimeRecorderMap.put(filename, recorder);
+            synchronized(sFilenameDictionaryUpdateControllerMap) {
+                recorder = new DictionaryUpdateController();
+                sFilenameDictionaryUpdateControllerMap.put(filename, recorder);
             }
         }
         return recorder;
@@ -189,7 +191,7 @@
         mContext = context;
         mIsUpdatable = isUpdatable;
         mBinaryDictionary = null;
-        mFilenameDictionaryTimeRecorder = getDictionaryTimeRecorder(filename);
+        mFilenameDictionaryUpdateController = getDictionaryUpdateController(filename);
         // Currently, only dynamic personalization dictionary is updatable.
         mDictionaryWriter = getDictionaryWriter(context, dictType, isUpdatable);
     }
@@ -347,6 +349,9 @@
             final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
             final int sessionId) {
         reloadDictionaryIfRequired();
+        if (isRegenerating()) {
+            return null;
+        }
         final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
         final AsyncResultHolder<ArrayList<SuggestedWordInfo>> holder =
                 new AsyncResultHolder<ArrayList<SuggestedWordInfo>>();
@@ -407,6 +412,9 @@
     }
 
     protected boolean isValidWordInner(final String word) {
+        if (isRegenerating()) {
+            return false;
+        }
         final AsyncResultHolder<Boolean> holder = new AsyncResultHolder<Boolean>();
         getExecutor(mFilename).executePrioritized(new Runnable() {
             @Override
@@ -432,7 +440,7 @@
      * dictionary exists, this method will generate one.
      */
     protected void loadDictionary() {
-        mPerInstanceDictionaryTimeRecorder.mLastUpdateRequestTime = SystemClock.uptimeMillis();
+        mPerInstanceDictionaryUpdateController.mLastUpdateRequestTime = SystemClock.uptimeMillis();
         reloadDictionaryIfRequired();
     }
 
@@ -443,8 +451,8 @@
     private void loadBinaryDictionary() {
         if (DEBUG) {
             Log.d(TAG, "Loading binary dictionary: " + mFilename + " request="
-                    + mFilenameDictionaryTimeRecorder.mLastUpdateRequestTime + " update="
-                    + mFilenameDictionaryTimeRecorder.mLastUpdateTime);
+                    + mFilenameDictionaryUpdateController.mLastUpdateRequestTime + " update="
+                    + mFilenameDictionaryUpdateController.mLastUpdateTime);
         }
 
         final File file = new File(mContext.getFilesDir(), mFilename);
@@ -482,8 +490,8 @@
     private void writeBinaryDictionary() {
         if (DEBUG) {
             Log.d(TAG, "Generating binary dictionary: " + mFilename + " request="
-                    + mFilenameDictionaryTimeRecorder.mLastUpdateRequestTime + " update="
-                    + mFilenameDictionaryTimeRecorder.mLastUpdateTime);
+                    + mFilenameDictionaryUpdateController.mLastUpdateRequestTime + " update="
+                    + mFilenameDictionaryUpdateController.mLastUpdateTime);
         }
         if (needsToReloadBeforeWriting()) {
             mDictionaryWriter.clear();
@@ -517,11 +525,11 @@
      */
     protected void setRequiresReload(final boolean requiresRebuild) {
         final long time = SystemClock.uptimeMillis();
-        mPerInstanceDictionaryTimeRecorder.mLastUpdateRequestTime = time;
-        mFilenameDictionaryTimeRecorder.mLastUpdateRequestTime = time;
+        mPerInstanceDictionaryUpdateController.mLastUpdateRequestTime = time;
+        mFilenameDictionaryUpdateController.mLastUpdateRequestTime = time;
         if (DEBUG) {
             Log.d(TAG, "Reload request: " + mFilename + ": request=" + time + " update="
-                    + mFilenameDictionaryTimeRecorder.mLastUpdateTime);
+                    + mFilenameDictionaryUpdateController.mLastUpdateTime);
         }
     }
 
@@ -530,14 +538,26 @@
      */
     public final void reloadDictionaryIfRequired() {
         if (!isReloadRequired()) return;
-        reloadDictionary();
+        if (setIsRegeneratingIfNotRegenerating()) {
+            reloadDictionary();
+        }
     }
 
     /**
      * Returns whether a dictionary reload is required.
      */
     private boolean isReloadRequired() {
-        return mBinaryDictionary == null || mPerInstanceDictionaryTimeRecorder.isOutOfDate();
+        return mBinaryDictionary == null || mPerInstanceDictionaryUpdateController.isOutOfDate();
+    }
+
+    private boolean isRegenerating() {
+        return mFilenameDictionaryUpdateController.mIsRegenerating.get();
+    }
+
+    // Returns whether the dictionary can be regenerated.
+    private boolean setIsRegeneratingIfNotRegenerating() {
+        return mFilenameDictionaryUpdateController.mIsRegenerating.compareAndSet(
+                false /* expect */ , true /* update */);
     }
 
     /**
@@ -550,39 +570,44 @@
         getExecutor(mFilename).execute(new Runnable() {
             @Override
             public void run() {
-                final long time = SystemClock.uptimeMillis();
-                final boolean dictionaryFileExists = dictionaryFileExists();
-                if (mFilenameDictionaryTimeRecorder.isOutOfDate() || !dictionaryFileExists) {
-                    // If the shared dictionary file does not exist or is out of date, the first
-                    // instance that acquires the lock will generate a new one.
-                    if (hasContentChanged() || !dictionaryFileExists) {
-                        // If the source content has changed or the dictionary does not exist,
-                        // rebuild the binary dictionary. Empty dictionaries are supported (in the
-                        // case where loadDictionaryAsync() adds nothing) in order to provide a
-                        // uniform framework.
-                        mFilenameDictionaryTimeRecorder.mLastUpdateTime = time;
+                try {
+                    final long time = SystemClock.uptimeMillis();
+                    final boolean dictionaryFileExists = dictionaryFileExists();
+                    if (mFilenameDictionaryUpdateController.isOutOfDate()
+                            || !dictionaryFileExists) {
+                        // If the shared dictionary file does not exist or is out of date, the
+                        // first instance that acquires the lock will generate a new one.
+                        if (hasContentChanged() || !dictionaryFileExists) {
+                            // If the source content has changed or the dictionary does not exist,
+                            // rebuild the binary dictionary. Empty dictionaries are supported (in
+                            // the case where loadDictionaryAsync() adds nothing) in order to
+                            // provide a uniform framework.
+                            mFilenameDictionaryUpdateController.mLastUpdateTime = time;
+                            writeBinaryDictionary();
+                            loadBinaryDictionary();
+                        } else {
+                            // If not, the reload request was unnecessary so revert
+                            // LastUpdateRequestTime to LastUpdateTime.
+                            mFilenameDictionaryUpdateController.mLastUpdateRequestTime =
+                                    mFilenameDictionaryUpdateController.mLastUpdateTime;
+                        }
+                    } else if (mBinaryDictionary == null ||
+                            mPerInstanceDictionaryUpdateController.mLastUpdateTime
+                                    < mFilenameDictionaryUpdateController.mLastUpdateTime) {
+                        // Otherwise, if the local dictionary is older than the shared dictionary,
+                        // load the shared dictionary.
+                        loadBinaryDictionary();
+                    }
+                    if (mBinaryDictionary != null && !mBinaryDictionary.isValidDictionary()) {
+                        // Binary dictionary is not valid. Regenerate the dictionary file.
+                        mFilenameDictionaryUpdateController.mLastUpdateTime = time;
                         writeBinaryDictionary();
                         loadBinaryDictionary();
-                    } else {
-                        // If not, the reload request was unnecessary so revert
-                        // LastUpdateRequestTime to LastUpdateTime.
-                        mFilenameDictionaryTimeRecorder.mLastUpdateRequestTime =
-                                mFilenameDictionaryTimeRecorder.mLastUpdateTime;
                     }
-                } else if (mBinaryDictionary == null ||
-                        mPerInstanceDictionaryTimeRecorder.mLastUpdateTime
-                                < mFilenameDictionaryTimeRecorder.mLastUpdateTime) {
-                    // Otherwise, if the local dictionary is older than the shared dictionary, load
-                    // the shared dictionary.
-                    loadBinaryDictionary();
+                    mPerInstanceDictionaryUpdateController.mLastUpdateTime = time;
+                } finally {
+                    mFilenameDictionaryUpdateController.mIsRegenerating.set(false);
                 }
-                if (mBinaryDictionary != null && !mBinaryDictionary.isValidDictionary()) {
-                    // Binary dictionary is not valid. Regenerate the dictionary file.
-                    mFilenameDictionaryTimeRecorder.mLastUpdateTime = time;
-                    writeBinaryDictionary();
-                    loadBinaryDictionary();
-                }
-                mPerInstanceDictionaryTimeRecorder.mLastUpdateTime = time;
             }
         });
     }
@@ -622,14 +647,15 @@
     }
 
     /**
-     * Time recorder for tracking whether the dictionary is out of date.
+     * For tracking whether the dictionary is out of date and the dictionary is regenerating.
      * Can be shared across multiple dictionary instances that access the same filename.
      */
-    private static class DictionaryTimeRecorder {
-        private volatile long mLastUpdateTime = 0;
-        private volatile long mLastUpdateRequestTime = 0;
+    private static class DictionaryUpdateController {
+        public volatile long mLastUpdateTime = 0;
+        public volatile long mLastUpdateRequestTime = 0;
+        public volatile AtomicBoolean mIsRegenerating = new AtomicBoolean();
 
-        private boolean isOutOfDate() {
+        public boolean isOutOfDate() {
             return (mLastUpdateRequestTime > mLastUpdateTime);
         }
     }
@@ -687,7 +713,7 @@
                         holder.set(mBinaryDictionary.isValidWord(word));
                     } else {
                         holder.set(((DynamicPersonalizationDictionaryWriter) mDictionaryWriter)
-                                .isInDictionaryForTests(word));
+                                .isInBigramListForTests(word));
                     }
                 }
             }
diff --git a/java/src/com/android/inputmethod/latin/personalization/DynamicPersonalizationDictionaryWriter.java b/java/src/com/android/inputmethod/latin/personalization/DynamicPersonalizationDictionaryWriter.java
index 0af028a..3050885 100644
--- a/java/src/com/android/inputmethod/latin/personalization/DynamicPersonalizationDictionaryWriter.java
+++ b/java/src/com/android/inputmethod/latin/personalization/DynamicPersonalizationDictionaryWriter.java
@@ -79,7 +79,7 @@
     public void addUnigramWord(final String word, final String shortcutTarget, final int frequency,
             final boolean isNotAWord) {
         if (mBigramList.size() > mMaxHistoryBigrams * 2) {
-            // Too many entries: just stop adding new vocabrary and wait next refresh.
+            // Too many entries: just stop adding new vocabulary and wait next refresh.
             return;
         }
         mExpandableDictionary.addWord(word, shortcutTarget, frequency);
@@ -90,7 +90,7 @@
     public void addBigramWords(final String word0, final String word1, final int frequency,
             final boolean isValid, final long lastModifiedTime) {
         if (mBigramList.size() > mMaxHistoryBigrams * 2) {
-            // Too many entries: just stop adding new vocabrary and wait next refresh.
+            // Too many entries: just stop adding new vocabulary and wait next refresh.
             return;
         }
         if (lastModifiedTime > 0) {
@@ -176,8 +176,8 @@
     }
 
     @UsedForTesting
-    public boolean isInDictionaryForTests(final String word) {
+    public boolean isInBigramListForTests(final String word) {
         // TODO: Use native method to determine whether the word is in dictionary or not
-        return mBigramList.containsKey(word);
+        return mBigramList.containsKey(word) || mBigramList.getBigrams(null).containsKey(word);
     }
 }
diff --git a/java/src/com/android/inputmethod/latin/utils/PrioritizedSerialExecutor.java b/java/src/com/android/inputmethod/latin/utils/PrioritizedSerialExecutor.java
index 5dc0b58..201a70d 100644
--- a/java/src/com/android/inputmethod/latin/utils/PrioritizedSerialExecutor.java
+++ b/java/src/com/android/inputmethod/latin/utils/PrioritizedSerialExecutor.java
@@ -16,8 +16,11 @@
 
 package com.android.inputmethod.latin.utils;
 
-import java.util.ArrayDeque;
 import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 /**
  * An object that executes submitted tasks using a thread.
@@ -27,19 +30,20 @@
 
     private final Object mLock = new Object();
 
-    // The default value of capacities of task queues.
-    private static final int TASK_QUEUE_CAPACITY = 1000;
     private final Queue<Runnable> mTasks;
     private final Queue<Runnable> mPrioritizedTasks;
     private boolean mIsShutdown;
+    private final ThreadPoolExecutor mThreadPoolExecutor;
 
     // The task which is running now.
     private Runnable mActive;
 
     public PrioritizedSerialExecutor() {
-        mTasks = new ArrayDeque<Runnable>(TASK_QUEUE_CAPACITY);
-        mPrioritizedTasks = new ArrayDeque<Runnable>(TASK_QUEUE_CAPACITY);
+        mTasks = new ConcurrentLinkedQueue<Runnable>();
+        mPrioritizedTasks = new ConcurrentLinkedQueue<Runnable>();
         mIsShutdown = false;
+        mThreadPoolExecutor = new ThreadPoolExecutor(1 /* corePoolSize */, 1 /* maximumPoolSize */,
+                0 /* keepAliveTime */, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
     }
 
     /**
@@ -59,7 +63,16 @@
     public void execute(final Runnable r) {
         synchronized(mLock) {
             if (!mIsShutdown) {
-                mTasks.offer(r);
+                mTasks.offer(new Runnable() {
+                    @Override
+                    public void run() {
+                        try {
+                            r.run();
+                        } finally {
+                            scheduleNext();
+                        }
+                    }
+                });
                 if (mActive == null) {
                     scheduleNext();
                 }
@@ -74,45 +87,36 @@
     public void executePrioritized(final Runnable r) {
         synchronized(mLock) {
             if (!mIsShutdown) {
-                mPrioritizedTasks.offer(r);
-                if (mActive ==  null) {
+                mPrioritizedTasks.offer(new Runnable() {
+                    @Override
+                    public void run() {
+                        try {
+                            r.run();
+                        } finally {
+                            scheduleNext();
+                        }
+                    }
+                });
+                if (mActive == null) {
                     scheduleNext();
                 }
             }
         }
     }
 
-    private boolean fetchNextTasks() {
-        synchronized(mLock) {
-            mActive = mPrioritizedTasks.poll();
-            if (mActive == null) {
-                mActive = mTasks.poll();
-            }
-            return mActive != null;
+    private boolean fetchNextTasksLocked() {
+        mActive = mPrioritizedTasks.poll();
+        if (mActive == null) {
+            mActive = mTasks.poll();
         }
+        return mActive != null;
     }
 
     private void scheduleNext() {
         synchronized(mLock) {
-            if (!fetchNextTasks()) {
-                return;
+            if (fetchNextTasksLocked()) {
+                mThreadPoolExecutor.execute(mActive);
             }
-            new Thread(new Runnable() {
-                @Override
-                public void run() {
-                    try {
-                        do {
-                            synchronized(mLock) {
-                                if (mActive != null) {
-                                    mActive.run();
-                                }
-                            }
-                        } while (fetchNextTasks());
-                    } finally {
-                        scheduleNext();
-                    }
-                }
-            }).start();
         }
     }
 
diff --git a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
index d605cdb..06c4271 100644
--- a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
+++ b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
@@ -84,29 +84,24 @@
     }
 
     /**
-     * @param checksContents if true, checks whether written words are actually in the dictionary
+     * @param checkContents if true, checks whether written words are actually in the dictionary
      * or not.
      */
     private void addAndWriteRandomWords(final String testFilenameSuffix, final int numberOfWords,
-            final Random random, final boolean checksContents) {
+            final Random random, final boolean checkContents) {
         final List<String> words = generateWords(numberOfWords, random);
         final UserHistoryPredictionDictionary dict =
                 PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(),
                         testFilenameSuffix /* locale */, mPrefs);
         // Add random words to the user history dictionary.
         addToDict(dict, words);
-        if (checksContents) {
+        if (checkContents) {
             try {
                 Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS));
             } catch (InterruptedException e) {
             }
-            // Limit word count to check when using a Java on memory dictionary.
-            final int wordCountToCheck =
-                    ExpandableBinaryDictionary.ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE ?
-                            numberOfWords : 10;
-            for (int i = 0; i < wordCountToCheck; ++i) {
+            for (int i = 0; i < numberOfWords; ++i) {
                 final String word = words.get(i);
-                // This may fail as long as we use tryLock on inserting the bigram words
                 assertTrue(dict.isInDictionaryForTests(word));
             }
         }