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

相关文章
相关标签/搜索