java - What "inconsistent synchronization" means? -
this java 1.6 class:
public class foo { private arraylist<string> names; public void scan() { if (names == null) { synchronized (this) { this.names = new arraylist<string>(); // fill array data } } } }
findbugs says:
inconsistent synchronization of com.xxx.foo.names; locked 40% of time
what mean , i'm doing wrong? i'm trying avoid problems when 2 or more clients call foo.scan()
@ same time.
it's beacuse synchronizing when set names
variable , not when read it. between read , write thread execute , you'd create 2 arraylists , fill them data, first 1 created gc'ed.
you need put synchronized block around read , write or add synchronized modifier method.
public class foo { private arraylist<string> names; public void scan() { synchronized (this) if (names == null) { this.names = new arraylist<string>(); // fill array data } } } }
Comments
Post a Comment