findbugs擬增長檢測線程非安全方面等編程防錯的規則

Findbugs擬增長檢測線程非安全方面等編程防錯的規則

-From 大師的郵件
http://en.wikipedia.org/wiki/Thread-safe 介紹了線程安全。線程安全問題每每比較隱晦,難重現。 Javadoc 中部分類及方法也沒標識 not thread safe
固然即便單行語句用了線程安全的函數,但可能依然沒法保證整塊代碼段的線程安全。
      Findbugs 增長一個自定義檢測器實踐,請參考: http://www.51testing.com/?uid-13997-action-viewspace-itemid-211893
 
Java 非線程安全大體分類
                                                                                                
1) 實現 singleton 模式不考慮併發
 
http://www.java.happycodings.com/Core_Java/code90.html 以下是一個線程安全版本。

The following code snippet shows an example of a thread-safe Singleton. html

package com.jgk.patterns.singleton;
 
public class JGKSingleton {
 
  /* Here is the instance of the Singleton */
  private static JGKSingleton instance_;
 
  /* Need the following object to synchronize */
  /* a block */
  private static Object syncObject_;
 
  /* Prevent direct access to the constructor
  private JGKSingleton() {
    super();
  }
 
 
  public static JGKSingleton getInstance() {
 
    /* in a non-thread-safe version of a Singleton   */
    /* the following line could be executed, and the */
    /* thread could be immediately swapped out */
    if (instance_ == null) {
 
      synchronized(syncObject_) {
 
        if (instance_ == null) {
           instance_ = new JGKSingleton();
        }
 
      }
 
    }
    return instance_;
  }
}
 
2) 調用靜態的 DateFormat , java.util.Calendar 及子類 (findbugs 已實現檢測 )

·    a static field of type java.util.Calendar or a subclass thereof java

相關文章
相關標籤/搜索