From the course: Advanced Java: Threads and Concurrency

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Synchronized versus concurrent collections

Synchronized versus concurrent collections - Java Tutorial

From the course: Advanced Java: Threads and Concurrency

Synchronized versus concurrent collections

- [Instructor] The synchronized wrapper on collections and concurrent collections both provide thread safety. However, the differences between them can be seen in terms of performance, scalability, and the mechanisms they use to achieve thread safety. Collections like ArrayList, HashMap and et cetera that are synchronized using the synchronized wrapper, like you see in this code, are not as fast as concurrent collections like CopyOnWriteArrayList and CopyOnWriteHashMap. The reason for this is the object-level locking that locks the entire collection prior to performing an operation on it. This lock does not also make the iterator associated with the collection thread safe, so the iteration code will have to be manually wrapped around a synchronized block to prevent from a concurrent modification exception being thrown. This could make an impact on an application in two ways. One way is because these locks need to be held for a considerably long period during iteration to prevent…

Contents