android - Hashtable : How can use it with 2/3 different thread? -
on android have 1 hashtable , 2 thread can access that. - ui thread access containskey, , put - other thread access containskey, , put , iterator
i know hashtable thread safe, sure ?
one thing don't understand : why application doesn't blow if 1 thread doesn't see 1 value has been removed hashtable , thread iterates on object ?
how can define hashtable thread-safe if iterator isn't ?
edit : tell in more specific way problem, becasue in other way i'm not clear. following reported code of concurrenthashmap :
public concurrenthashmap<string,result> holder = new concurrenthashmap<string,result>(); class result { public float goal = 0; public float min = 0; public float max = 0; public float seconds = 0; //lastaccess indicate time of last access of result public float lastaccess = 0; public boolean triggered = false; }
now 1 thread b iterates every "x" seconds result object , each key stored in holder concurrethashmap .
other thread set new result new key or edit result existing one.
i want operation of update result in atomic way, object inside hashmap scares me.
ux thread remove item holder every x second. operation in way : created
object mlock = new object();
in thread b
syncrhonized(mlock) { //all thread b function. // iterate on of holder item }
in other thread c when have call holder.clear() have done in way :
syncronized(mlock) { holder.clear(); }
- is correct way prevent thread b iterate on holder inconsistent data ?
it thread safe in high concurrent scenarios recommended use concurrenthashmap
can see on official documentation
Comments
Post a Comment