Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashMap, SparseArray in Android: multithread a concern?

I know neither HashMap nor SparseArray are threadsafe. Do I have to worry about that if I have a central data repository as HashMap which can be accessed by the activity and potentially by an AsyncTask?

Would it be advisable to use a HashTable or better ConcurrentHashMap to be on the safe side?

like image 687
Gunnar Bernstein Avatar asked Sep 28 '22 17:09

Gunnar Bernstein


1 Answers

Yes, you'll have to worry about thread safety. You can either use a HashTable, ConcurrentHashMap, or a synchronization block whenever you're doing IO around the HashMap or SparseArray. The synchronization block acts a bit different and might be slower, but for a simple application, this likely will not be an issue.

like image 137
Al Wang Avatar answered Oct 03 '22 07:10

Al Wang