CopyOnWriteArrayList in Java
CopyOnWriteArrayList is a thread-safe variant of the ArrayList class in Java. It allows multiple threads to read the list simultaneously without requiring any synchronization, but when a thread needs to modify the list, it makes a new copy of the underlying array, modifies it, and then sets it back, ensuring that no other thread can see the changes until they have been completed.
This strategy of copying the entire list every time it is modified can be expensive in terms of memory usage and CPU time, but it can be useful in certain situations where reads are much more common than writes, and where it is important to ensure that modifications made by one thread do not interfere with the data being read by other threads.
CopyOnWriteArrayList is useful in situations where a list is used mostly for reading, such as in a cache or a read-heavy data structure. However, it is not suitable for situations where frequent writes or modifications are needed, as the cost of copying the entire list every time can become prohibitively expensive. In those situations, other thread-safe list implementations, such as ConcurrentLinkedDeque or ConcurrentHashMap, may be more appropriate.
It is important to note that while CopyOnWriteArrayList provides thread-safety guarantees for individual operations, it does not provide atomicity guarantees for sequences of operations. For example, if two threads modify the list at the same time, it is possible for one thread's changes to be lost. Therefore, it is important to carefully consider the use case and requirements when choosing a thread-safe list implementation.
CopyOnWriteArrayList in Java:
javaimport java.util.concurrent.CopyOnWriteArrayList;
public class Example {
public static void main(String[] args) {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
list.add("apple");
list.add("banana");
list.add("cherry");
// multiple threads can read the list simultaneously
Thread reader1 = new Thread(() -> {
for (String item : list) {
System.out.println("Reader 1: " + item);
}
});
Thread reader2 = new Thread(() -> {
for (String item : list) {
System.out.println("Reader 2: " + item);
}
});
reader1.start();
reader2.start();
// a thread modifying the list makes a new copy of the underlying array
// before performing the modification, ensuring thread-safety
Thread writer = new Thread(() -> {
list.add("orange");
});
writer.start();
}
}
In this example, a CopyOnWriteArrayList is created and populated with three items. Two threads are then created to read the list simultaneously. Because CopyOnWriteArrayList is thread-safe, there is no need for any synchronization between the two reader threads.
A third thread is then created to modify the list by adding an item. Because the list is thread-safe, the writer thread can modify the list without worrying about synchronization issues with the reader threads.
When this code is run, both reader threads will print out the three items in the list (apple, banana, cherry), and the writer thread will add an item (orange) to the list. Because CopyOnWriteArrayList makes a new copy of the underlying array before each modification, the modifications made by the writer thread will not be visible to the reader threads until they have completed. Therefore, both reader threads will only print out the original three items and not the new item added by the writer thread.
Comments
Post a Comment