Findbugs拟增加检测线程非安全方面等编程防错的规则
-From 大师的邮件
http://en.wikipedia.org/wiki/Thread-safe
介绍了线程安全。线程安全问题往往比较隐晦,难重现。
Javadoc
中部分类及方法也没标识
not thread safe
。
当然即使单行语句用了线程安全的函数,但可能依然无法保证整块代码段的线程安全。
一 Java
非线程安全大致分类
1)
实现
singleton
模式不考虑并发
The following code snippet shows an example of a thread-safe Singleton.
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
转载于:https://blog.51cto.com/tianya23/291501