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 names variable and not when you read it. So between the read and the write another thread could execute and you'd create two ArrayLists and fill them with data, the first one created would get GC'ed.

You need to put the synchronized block around the read and the write or add the synchronized modifier to the method.

public class Foo {
  private ArrayList<String> names;
    public void scan() {
      synchronized (this)
        if (names == null) {
           this.names = new ArrayList<String>();
           // fill the array with data
         }
       }
     }
  }



回答2:


The first time you reference names inside scan is outside of synchronized block.
E.g., if scan is called twice from two different threads and names is null, it may go like this

  1. if (names == null) from the first thread is processed (to true).
  2. if (names == null) from the second thread is processed (to true).
  3. First thread enters synchronized block, assignes names and leaves synchronized block.
  4. Second thread enters synchronized block, assignes names and leaves synchronized block.

Now, names is initialized twice. And this is only one possible scenario where you get unexpected results.



来源:https://stackoverflow.com/questions/4187286/what-inconsistent-synchronization-means

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!