java - Synchronized Non-Final List -
this question has answer here:
ok understand it, best create final static
object use synchronization.
however, read if object reference doesn't change, won't have concurrency issues.
do following code violate synchronicity?
class foo { private static arraylist<client> clients = null; public foo() { clients = new arraylist<>(); //add stuff list here.. } public void addclient(client c) { synchronized(clients) { clients.add(c); } } }
do have make clients final or create final object if clients arraylist
never exposed directly (only other through getters)? in other words, never provide set method clients array reference never changes.
anyone creating instance of foo new foo()
overrides clients array. not thread safe
Comments
Post a Comment