From the course: Advanced Java: Threads and Concurrency
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
The need for concurrent collections - Java Tutorial
From the course: Advanced Java: Threads and Concurrency
The need for concurrent collections
- [Instructor] Java provides two main approaches to ensure thread safety in collections, synchronized collections and concurrent collections. What does synchronized collections really mean? Here the collection itself is not synchronized. Instead, it is wrapped in a synchronized wrapper so that all the methods of the collection object are synchronized. Then they can perform thread safe operations. What really happens here is that the synchronized collection delegates all work to the wrapped collection. This is achieved with the help of factory methods that are in this format. So we have, for example, methods to wrap collections like lists, maps, sets, and et cetera that return a thread safe collection. For example, this code wraps an error list and returns a thread safe list, and this code returns the thread safe map. What really happens here is the wrapper keeps a mutual exclusion lock and locks the collection before each operation. Further, this is object level synchronization, which…