synchronization

What “inconsistent synchronization” means?

依然范特西╮ 提交于 2020-08-22 11:49:05
问题 This is my 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 the array with data } } } } Findbugs says: Inconsistent synchronization of com.XXX.Foo.names; locked 40% of time What does it mean and what I'm doing wrong? I'm trying to avoid problems when two or more clients call Foo.scan() at the same time. 回答1: It's beacuse you are only synchronizing when you set the