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
Post a Comment