First we need to understand the purpose of the author. The authorβs goal is to create a thread-safe List tool that is safe for all methods, including putifAbsent methods.
And ListHelper may also have some other methods as follows:
public void addList(E x) { list.add(x); } public void removeList(E x) { list.remove(x); }
If some thread calls removeList and the second thread calls addList and the third one calls putIfAbsent, then because they are different locks, an error occurs.
source share