Java


JAVA.CONCURRENCY.VOLATILE : Useless volatile Modifier (Java)

要旨

A field is declared volatile.

Concurrency is an important but complex aspect of modern software. As a consequence, it is often used in an incorrect way, also because the subtleties of the Java memory model are not always understood. This checker identifies a large class of common programming errors due to incorrect uses of concurrency primitives, such as incorrect implementations of the singleton pattern and incorrect uses of the volatile field modifier, whose goal is to publish a field update to all executing cores. The latter, however, has a cost in terms of execution time.

プロパティ

クラス名 Useless volatile Modifier (Java)
日本語クラス名 Useless volatile Modifier (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.CONCURRENCY.VOLATILE
カテゴリー
CWE CWE:567 Unsynchronized Access to Shared Data in a Multithreaded Context
  CWE:662 Improper Synchronization
CERT-Java CERT-Java:VNA00-J Ensure visibility when accessing shared primitive variables
  CERT-Java:VNA03-J Do not assume that a group of calls to independently atomic methods is atomic
対応言語 Java で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Useless volatile Modifier (Java)"

import java.util.HashMap;
import java.util.Map;

public class TestConcurrency {
  private static TestConcurrency instance;
  private final static Object lock1 = new Object();
  private volatile String lock2 = "lock";                      // Useless volatile Modifier (Java) warning issued here
  private String lock3 = new String("lock");
  private String lock4 = new String("lock").intern();
  private volatile Map<String, Integer> map = new HashMap<>(); // Useless volatile Modifier (Java) warning issued here (multiple instances)

  private TestConcurrency() {}

  public static TestConcurrency getInstance1() {
      if (instance == null)
          instance = new TestConcurrency(); // Double-Checked Locking (Java) warning issued here (Java)
      return instance;
  }

  public static TestConcurrency getInstance2() {
      synchronized (lock1) {
          if (instance == null)
              instance = new TestConcurrency();
      }
      return instance;
  }

  public static TestConcurrency getInstance3() {
      if (instance == null)
          synchronized (lock1) {
              if (instance == null)
                  instance = new TestConcurrency();
          }
      return instance;
  }

  private int counter;

  private int next() {
      map.put(String.valueOf(++counter), counter);
      return counter;
  }

  public int step(int i) {
      synchronized (lock1) {              // Useless Synchronization (Java) warning issued here
          i++;
      }
      return i;
  }

  public int test1() {
      synchronized (lock2) {              // Synchronization on Interned String (Java) warning issued here
          return next();
      }
  }

  public int test2() {
      synchronized (lock3) {
          return next();
      }
  }

  public int test3() {
      synchronized (lock4) {              // Synchronization on Interned String (Java) warning issued here
          return next();
      }
  }

  public int callTest6() {
      return test6(lock2);
  }

  public int test6(String s) {
      synchronized (s) {                   // Synchronization on Interned String (Java) warning issued here
          return next();
      }
  }
}
public class MyClass  {

  private volatile int[] arr; /* "Useless volatile Modifier (Java)" 
                               * warning issued here: only the array reference 
                               * is safely published, not the elements of the array.

  public MyClass() {
    arr = new int[5];
  }

  // ...
}
        

解決法

Check if the warnings correspond to actual possible errors for a concurrent execution of the program.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。