user1442034
user1442034

Reputation: 69

Difference between Normal Map and collections.synchronizedmap

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

Upvotes: 5

Views: 637

Answers (7)

Tom Carchrae
Tom Carchrae

Reputation: 6486

http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#synchronizedCollection(java.util.Collection)

A synchronized map is a thread-safe map, meaning that read/write operations are thread-safe.

Upvotes: 1

Leonidos
Leonidos

Reputation: 10518

There is no normal Map. You cannot create just a Map (new Map() wont compile). Map is a common interface to access data in different kinds of maps (hashMap, synchronizedMap, WeakHashMap...). Read any Java book, Collections chapter.

Using Map as common interface allows you to write data manipulation algorinthms which will work for all types of Map implementations. So you can switch implementation (maybe you realized that you need synchronized map here and not just HashMap) and your code will work.

synchronizedMap is just a wrapper. It wraps all data access functions of underlying Map and makes them thread safe.

In your case you have WeakHashMap which is "real" map. WeakHashMap defines how map stores data (it uses hashes for keys and weakRefences for values). Then you wrap it in synchronizedMap which makes your map thread safe.

Upvotes: 1

Mirko Adari
Mirko Adari

Reputation: 5103

Collections.synchronizedMap() is a very poor way to achieve thread safety. It takes a lock on each method, so at most one thread can access your map. It doesn't have support for atomic operations as compare-and-set. Also you have to remember to synchronize on the object yourself when iterating over the keys/values or else it looses all of it's magic.

Look at ConcurrentHashMap, if you need a thread-safe Map.

Upvotes: 1

Akhil Raina
Akhil Raina

Reputation: 379

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)

Upvotes: 2

rai.skumar
rai.skumar

Reputation: 10667

Collections utility class provides static methods to creat thread safe collection(List, Set, Queue, Map ). So to convert any thread unsafe collection into a thread safe one; you call synchronized method on Collections and then pass your collection as parameter.

A thread safe collection can be accessed by only one thread at a time.

URL : http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html

Upvotes: 1

Denis Tulskiy
Denis Tulskiy

Reputation: 19167

Collections.synchronizedMap pretty much wraps your Map object so that every call to map methods is synchronized, meaning only one thread at a time can update or query the map.

Upvotes: 1

Manoj Pal
Manoj Pal

Reputation: 180

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.

Upvotes: 1

Related Questions