android - How to implement a thread safe central object/list that can be accessed without concurrency exceptions? -
i'm having trouble implementing thread safe way centrally update , access list of class objects in android app. open type of question, not lot of source code can given.
basically have app activity , service. service implements udp broadcasting task , udp broadcast listening task. broadcasted information json serialized copy of class represents device. e.g. uuid, ip, administrative info user set name, description, etc... json stored in udp packet , sent, udp packet received , de-serialized , processed. de-serialized class stored in hashtable , current instance belongs service. e.g. wants access data has go through service. whole thing async.
the activity binds service (via , extension of binder) can call service methods, start/stop udp tasks. activity listen android intents service issue when updates or new device data has been received, , ui display information related received udp packet data. note again the packet data stored in container class belonging service.
the problem can't figure out suitable way make hashtable of received data thread safe. java.util.concurrentmodificationexception errors when data grabbed service method, processed. if data updated while i'm processing in loop (iterator or for), concurrentmodificationexception happen. know where, when , why, using lock() or reentrantlock() typically leveraged within method call of class has data locked, not on single point data that's returned processing outside container class. like: (where i'm using syncronized, not reentrantlock(), example)
public class samplelockclass { private hashtable<string, string> sampledata = new hashtable<string, string>(); public samplelockclass() {} public synchronized put(string s1, string s2) { this.sampledata().put(s1, s2); } public synchronized hashtable<string, string> getall() { return this.sampledata; // returned processing outside class } }
in case, getall() method returning sampledata hashtable needed processing outside class itself. reason data passed other apis i'm leveraging, , they're not compatible approach. e.g. expect there single, thread safe copy use.
maybe dumb or non-issue, how go making returned sampledata thread safe period needed? note right service write sampledata. else read-only, , try commit updates sampledata activity through commservice via intent.
would safer try make copy of hashtable sampledata each get() method?
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/concurrenthashmap.html
a hash table supporting full concurrency of retrievals , adjustable expected concurrency updates. class obeys same functional specification hashtable, , includes versions of methods corresponding each method of hashtable.
retrieval operations (including get) not block, may overlap update operations (including put , remove). retrievals reflect results of completed update operations holding upon onset. aggregate operations such putall , clear, concurrent retrievals may reflect insertion or removal of entries.
the allowed concurrency among update operations guided optional concurrencylevel constructor argument (default 16), used hint internal sizing. table internally partitioned try permit indicated number of concurrent updates without contention. because placement in hash tables random, actual concurrency vary.
public collection values()
returns collection view of values contained in map. collection backed map, changes map reflected in collection, , vice-versa. collection supports element removal, removes corresponding mapping map, via iterator.remove, collection.remove, removeall, retainall, , clear operations. not support add or addall operations. view's iterator "weakly consistent" iterator never throw concurrentmodificationexception, , guarantees traverse elements existed upon construction of iterator, , may (but not guaranteed to) reflect modifications subsequent construction.
Comments
Post a Comment