Findbugs擬增長檢測線程非安全方面等編程防錯的規則
-From 大師的郵件
固然即便單行語句用了線程安全的函數,但可能依然沒法保證整塊代碼段的線程安全。
一 Java
非線程安全大體分類
1)
實現
singleton
模式不考慮併發
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