Prevent ConcurrentModificationExceptions

Use sets backed by ConcurrentHashMaps instead of HashSets, and
CopyOnWriteArrayLists instead of ArrayLists, to prevent concurrent
exceptions if listeners try to remove themselves in callbacks while
iterating over the listeners.

Bug:16325026
Change-Id: I55e081eda6ba19fa466bbf019c648bbdaf833c33
This commit is contained in:
Jay Shrauner
2014-08-15 09:23:07 -07:00
parent d438deffad
commit 229e3820dc
5 changed files with 69 additions and 63 deletions

View File

@@ -36,8 +36,13 @@ import java.util.concurrent.ConcurrentHashMap;
* @hide
*/
final class ConnectionServiceAdapter implements DeathRecipient {
/**
* ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
* load factor before resizing, 1 means we only expect a single thread to
* access the map so make only a single shard
*/
private final Set<IConnectionServiceAdapter> mAdapters = Collections.newSetFromMap(
new ConcurrentHashMap<IConnectionServiceAdapter, Boolean>(2));
new ConcurrentHashMap<IConnectionServiceAdapter, Boolean>(8, 0.9f, 1));
ConnectionServiceAdapter() {
}
@@ -53,7 +58,7 @@ final class ConnectionServiceAdapter implements DeathRecipient {
}
void removeAdapter(IConnectionServiceAdapter adapter) {
if (mAdapters.remove(adapter)) {
if (adapter != null && mAdapters.remove(adapter)) {
adapter.asBinder().unlinkToDeath(this, 0);
}
}