java - Can I synchronize a block of code using an instance of ArrayList? -


i know there 1 way synchronize variables in java. using lock.

public class mslunch {     private arraylist<clienthandler> clients = new arraylist<clienthandler>();      private object lock1 = new object();      public void inc1() {         synchronized(lock1) {             //maybe add array list         }     } } 

what curious whether can way below? if yes, reference?

public class mslunch {     private arraylist<clienthandler> clients = new arraylist<clienthandler>();      public void inc1() {         synchronized (clients) {             //maybe add array list         }     } } 

yes can should not... java has concurrent list utilities in java.util.concurrent package.use copyonwritearraylist instead of using synchronize keyword performance wise better use concurrent utility.that save trouble of handling locks in code.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -