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