In some android open source code I found
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new
WeakHashMap<ImageView, String>());
can any one explain me difference between Normal Map
and collections.synchronizedmap
The regular Map implementations in java.util package are not thread safe. This means that if multiple threads are doing get()
or put()
operations on the same Map, it may result in race conditions or inconsistent data in the Map.
To use an existing Map in a multi-threaded environment, you can get a synchronized instance of the same by calling Collections.synchronizedMap()
. On such instances, most of the methods like get()
, put
and keyset()
are synchronized and can be safely used concurrently.
For more information on this, refer to
http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedMap(java.util.Map)
We are using "Synchronized" to ensure that two concurrently-executing threads or processes do not execute specific portions of a program at the same time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With