public class UnModifiableCollection {
private List<String> names = new ArrayList<>();
public void testConcurrency() {
names.add("1");
names.add("2");
names.add("3");
names.add("4");
Collection < String > dynamicView = Collections.unmodifiableCollection(names);
for (String s: dynamicView) { (1)
System.out.println("s = " + s);
names.remove(0); (2)
}
}
public static void main(String[] args) {
UnModifiableCollection test = new UnModifiableCollection();
test.testConcurrency();
}
}
What is does Collections.unmodifiableCollection do
Carvia Tech | December 04, 2019 | 1 min read | 189 views | Multithreading and Concurrency
What does Collections.unmodifiableCollection() do? Is it safe to use the collection returned by this method in a multi-threading environment?
Collections.unmodifiableCollection() returns a unmodifiable dynamic view of underlying data structure. Any attempt direct or via iterator to modify this view throws UnsupportedOperationException, but any changes made in the underlying data structure will be reflected in the view.
This method is no substitute for the other thread safety techniques because iterating over a collection using this view may throw ConcurrentModificationException if original collection is structurally modified during the iteration. |
For example, the following code will throw ConcurrentModificationException in the for loop:
1 | this will throw ConcurrentModification in 2nd iteration |
2 | this is the culprit line modifying the underlying collection |
Hence, external synchronization is must if we are going to modify the underlying collection, even if you are using Collections.unmodifiableCollection()
.
Top articles in this category:
- What is difference between Callable and Runnable Interface?
- What is Deadlock in Java? How to troubleshoot and how to avoid deadlock
- What is polymorphism in Java OOP
- What is difference between sleep() and wait() method in Java?
- What is Double Checked Locking Problem in Multi-Threading?
- What is volatile keyword in Java
- What is difference between ExecutorService submit and execute method
Find more on this topic:

Core Java
Core Java - OOP Concepts, Garbage Collection, Multi-threading, Collections Framework, Java 8 Features, Lambda Functions, Streams.
Last updated 1 week ago
Recommended books for interview preparation:
Similar Posts
- Http download using Java NIO FileChannel
- CRC32 checksum calculation Java NIO
- Set precision and scale for a double value in java
- Difference between HashMap, LinkedHashMap and TreeMap
- What is difference between ExecutorService submit and execute method
- What is left shift right shift and unsigned rght shift operator in Java
- What happens when wait() & notify() method are called
- can we write a java method that swaps two integers
- Find missing numbers in 4 billion unique numbers with 50MB RAM
- How to configure custom ThreadPool for Java 8 Stream API parallel operations
Enter your email address to subscribe to this blog and receive notifications of new posts by email.