Spark-Beginner
Spark-Beginner

Reputation: 1364

Difference between Wrappers and Concurrent collection

we can synchronize a collection by using 'collections.synchronizedCollection(Collection c)' or 'collections.synchronizedMap(Map c)' and we can also use java concurrent API like ConcurrentHashMap or ArrayQueue or BlockingQueue.

Is there any difference in synchronization level between these two way of getting synchronized collections or these are almost same?

could any one explain?

Upvotes: 1

Views: 956

Answers (2)

Spark-Beginner
Spark-Beginner

Reputation: 1364

so now as per my understandings synchronized way is a wrapper and blocks whole collection object and on other hand in concurrent way only objects inside collection get synchronized and we can access 2 or more elements of a collection at same time

Upvotes: 0

TwoThe
TwoThe

Reputation: 14309

Yes: speed during massive parallel processing.

This can be illustrated in a very simple way: Imagine that 100 Threads are waiting to take something out of a collection.

  • The synchronized way: 99 Threads a put to sleep and 1 Thread gets its value.

  • The concurrent way: 100 Threads get their value immediately, none is put on hold.

Now the second method takes a little more time than a simple get, but as soon as a minimum like 2 Threads are using it on a constant basis, it is well worth the time you save thanks to concurrent execution.

Upvotes: 2

Related Questions