diff options
author | 2011-08-22 23:05:51 -0700 | |
---|---|---|
committer | 2011-08-22 23:05:51 -0700 | |
commit | d6834c8125031b3ba0c1f6671debb15a4232ff77 (patch) | |
tree | f1eac790bbb8b075e0136ddf979e6196d8305dda /java/src/com/android/inputmethod/latin/StringBuilderPool.java | |
parent | 6dde878d515f7bf5268d16a8fe4921d8821c5ae7 (diff) | |
parent | a6e912cf9849f5c979303042ce83820a8dc560d0 (diff) | |
download | latinime-d6834c8125031b3ba0c1f6671debb15a4232ff77.tar.gz latinime-d6834c8125031b3ba0c1f6671debb15a4232ff77.tar.xz latinime-d6834c8125031b3ba0c1f6671debb15a4232ff77.zip |
Merge "Fix a bug with the string pool."
Diffstat (limited to 'java/src/com/android/inputmethod/latin/StringBuilderPool.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/StringBuilderPool.java | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/java/src/com/android/inputmethod/latin/StringBuilderPool.java b/java/src/com/android/inputmethod/latin/StringBuilderPool.java index 66f123731..a663ed43e 100644 --- a/java/src/com/android/inputmethod/latin/StringBuilderPool.java +++ b/java/src/com/android/inputmethod/latin/StringBuilderPool.java @@ -26,12 +26,20 @@ import java.util.List; public class StringBuilderPool { // Singleton private static final StringBuilderPool sInstance = new StringBuilderPool(); + private static final boolean DEBUG = false; private StringBuilderPool() {} - // TODO: Make this a normal array with a size of 20 + // TODO: Make this a normal array with a size of 20, or a ConcurrentQueue private final List<StringBuilder> mPool = Collections.synchronizedList(new ArrayList<StringBuilder>()); public static StringBuilder getStringBuilder(final int initialSize) { + // TODO: although the pool is synchronized, the following is not thread-safe. + // Two threads entering this at the same time could take the same size of the pool and the + // second to attempt removing this index from the pool would crash with an + // IndexOutOfBoundsException. + // At the moment this pool is only used in Suggest.java and only in one thread so it's + // okay. The simplest thing to do here is probably to replace the ArrayList with a + // ConcurrentQueue. final int poolSize = sInstance.mPool.size(); final StringBuilder sb = poolSize > 0 ? (StringBuilder) sInstance.mPool.remove(poolSize - 1) : new StringBuilder(initialSize); @@ -40,6 +48,12 @@ public class StringBuilderPool { } public static void recycle(final StringBuilder garbage) { + if (DEBUG) { + final int gid = garbage.hashCode(); + for (final StringBuilder q : sInstance.mPool) { + if (gid == q.hashCode()) throw new RuntimeException("Duplicate id " + gid); + } + } sInstance.mPool.add(garbage); } |