Will both threads have access to both (add and remove) api in at the same time.
The answer is no.
If you have the opportunity to take a look at the source code of Collections.synchronizedList(List) , you will see that the method creates an instance of a static inner class called SynchronizedList or SynchronizedRandomAccessList , depending on the type of List you are sending as an argument.
Now both of these static inner classes extend a common class called SynchronizedCollection , which supports the mutex object, on which all method operations are synchronized to
This mutex object is assigned to this , which essentially means that the mutex object is the same returned instance .
Since the add() and remove() methods are executed under
synchronized(mutex) { }
block, the thread that executes add (and blocks the lock on mutex ) will not allow another thread to perform remove (by placing the lock on the same mutex ), since the first one has mutex already blocked . The last thread will wait until the lock received by the former thread on mutex receives .
So yes add() and remove() are mutually exclusive
source share