Had there been a
public synchronized void deletePerson(Person p)
{ mySet.remove();}
then too it would remain threadsafe?
Had there been a
public synchronized void deletePerson(Person p)
{ mySet.remove();}
then too it would remain threadsafe?
This class is threadsafe because there is only one mutable field in it (mySet
) , it is private
and all accesses to it are synchronized.
Yes, public synchronized void deletePerson(Person p) { mySet.delete();}
would still keep this class thread-safe.
Also, note that the reference to mySet
is not escaping from this class. Which is also important.
Since mySet
is private and not exposed outside the class through a getMySet
method, you can access to the state of the object only with the methods addPerson
, containsPerson
and deletePerson
.
Since these 3 methods are synchronized, only one of them can access to the instance of the class (and change its state) at any given time, so the class is Thread Safe.