Make some usages of SparseArray thread-safe

This is a follow up of I39d9bf1a7c272eb16d6ed4698f52457579b40f10.

Change-Id: Ic3288e491175f9a85679a21064093f9db5a65466
main
Tadashi G. Takaoka 2012-08-20 14:37:16 +09:00
parent 4c6db0592e
commit 8ce351a827
1 changed files with 16 additions and 11 deletions

View File

@ -64,19 +64,22 @@ 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) {
synchronized(mDicTraverseSessions) {
DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId); DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId);
if (traverseSession == null) { if (traverseSession == null) {
synchronized(mDicTraverseSessions) {
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;
} }
}
/** /**
* Constructor for the binary dictionary. This is supposed to be called from the * Constructor for the binary dictionary. This is supposed to be called from the
@ -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) {
final DicTraverseSession traverseSession = mDicTraverseSessions.valueAt(index);
if (traverseSession != null) { if (traverseSession != null) {
traverseSession.close(); 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;