From 8ce351a8275f0ad73cdd642e8b46a430b072e8ef Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Mon, 20 Aug 2012 14:37:16 +0900 Subject: [PATCH] Make some usages of SparseArray thread-safe This is a follow up of I39d9bf1a7c272eb16d6ed4698f52457579b40f10. Change-Id: Ic3288e491175f9a85679a21064093f9db5a65466 --- .../inputmethod/latin/BinaryDictionary.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 726c44cc1..2d9e0ce2c 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -64,18 +64,21 @@ public class BinaryDictionary extends Dictionary { private final SparseArray mDicTraverseSessions = new SparseArray(); + + // TODO: There should be a way to remove used DicTraverseSession objects from + // {@code mDicTraverseSessions}. private DicTraverseSession getTraverseSession(int traverseSessionId) { - DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId); - if (traverseSession == null) { - synchronized(mDicTraverseSessions) { + synchronized(mDicTraverseSessions) { + DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId); + if (traverseSession == null) { traverseSession = mDicTraverseSessions.get(traverseSessionId); if (traverseSession == null) { traverseSession = new DicTraverseSession(mLocale, mNativeDict); mDicTraverseSessions.put(traverseSessionId, traverseSession); } } + return traverseSession; } - return traverseSession; } /** @@ -209,18 +212,20 @@ public class BinaryDictionary extends Dictionary { } @Override - public synchronized void close() { - for (int i = 0; i < mDicTraverseSessions.size(); ++i) { - final int key = mDicTraverseSessions.keyAt(i); - final DicTraverseSession traverseSession = mDicTraverseSessions.get(key); - if (traverseSession != null) { - traverseSession.close(); + public void close() { + synchronized (mDicTraverseSessions) { + final int sessionsSize = mDicTraverseSessions.size(); + for (int index = 0; index < sessionsSize; ++index) { + final DicTraverseSession traverseSession = mDicTraverseSessions.valueAt(index); + if (traverseSession != null) { + traverseSession.close(); + } } } closeInternal(); } - private void closeInternal() { + private synchronized void closeInternal() { if (mNativeDict != 0) { closeNative(mNativeDict); mNativeDict = 0;