Let's say, We're trying to add several elements into an ArrayList
. At the same time, the same list is being searched for list.contains(givenElement)
.
Then, how do we achieve thread-safety without locking the whole list
object?
In other words, how can multiple threads access this list and perform operations atomically
? I would like to know how to achieve this using AtomicReference
.
I can create an AtomicReference
variable with the list
loaded in it. But, then how do I do contains()
operation atomically without doing compareAndSet()
on whole list
?
Note that I'm looking for solutions other than using thread-safe collections.