am 291de5e1: am cb2362ce: Merge "Make some usages of SparseArray thread-safe" into jb-mr1-dev

* commit '291de5e1c4b5c4be906fdaa13470e4ae6c307779':
  Make some usages of SparseArray thread-safe
main
Tadashi G. Takaoka 2012-08-19 23:26:46 -07:00 committed by Android Git Automerger
commit 63b842f572
1 changed files with 16 additions and 11 deletions

View File

@ -64,18 +64,21 @@ public class BinaryDictionary extends Dictionary {
private final SparseArray<DicTraverseSession> mDicTraverseSessions = private final SparseArray<DicTraverseSession> mDicTraverseSessions =
new SparseArray<DicTraverseSession>(); new SparseArray<DicTraverseSession>();
// TODO: There should be a way to remove used DicTraverseSession objects from
// {@code mDicTraverseSessions}.
private DicTraverseSession getTraverseSession(int traverseSessionId) { private DicTraverseSession getTraverseSession(int traverseSessionId) {
DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId); synchronized(mDicTraverseSessions) {
if (traverseSession == null) { DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId);
synchronized(mDicTraverseSessions) { if (traverseSession == null) {
traverseSession = mDicTraverseSessions.get(traverseSessionId); traverseSession = mDicTraverseSessions.get(traverseSessionId);
if (traverseSession == null) { if (traverseSession == null) {
traverseSession = new DicTraverseSession(mLocale, mNativeDict); traverseSession = new DicTraverseSession(mLocale, mNativeDict);
mDicTraverseSessions.put(traverseSessionId, traverseSession); mDicTraverseSessions.put(traverseSessionId, traverseSession);
} }
} }
return traverseSession;
} }
return traverseSession;
} }
/** /**
@ -209,18 +212,20 @@ public class BinaryDictionary extends Dictionary {
} }
@Override @Override
public synchronized void close() { public void close() {
for (int i = 0; i < mDicTraverseSessions.size(); ++i) { synchronized (mDicTraverseSessions) {
final int key = mDicTraverseSessions.keyAt(i); final int sessionsSize = mDicTraverseSessions.size();
final DicTraverseSession traverseSession = mDicTraverseSessions.get(key); for (int index = 0; index < sessionsSize; ++index) {
if (traverseSession != null) { final DicTraverseSession traverseSession = mDicTraverseSessions.valueAt(index);
traverseSession.close(); if (traverseSession != null) {
traverseSession.close();
}
} }
} }
closeInternal(); closeInternal();
} }
private void closeInternal() { private synchronized void closeInternal() {
if (mNativeDict != 0) { if (mNativeDict != 0) {
closeNative(mNativeDict); closeNative(mNativeDict);
mNativeDict = 0; mNativeDict = 0;