Say I have an array [a, b, c, d]
Theme A wants to add a new element e to the set. CopyOnWriteArrayList creates a new array, copies all the values ββfrom the old array, adds a new e element, and then updates the link to the new array with the e element.
While thread A copies the values, thread B also wants to add a new element f . Thus, it copies all values ββwithout e adds f , and then updates the array reference.
In this case, the array may not have an element e .
How is thread safe?
source share