线程安全框架 [英] Thread Safety framework

查看:84
本文介绍了线程安全框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下类不是线程安全的(如所示)代码不是线程安全的)

The following class is not thread-safe (as proven in Proving the following code not thread safe )

是否有一个框架可以帮助进行编译时/运行时分析,并告诉我们以下内容不是线程安全的?

Is there a framework out there that can help with either compile time / run time analysis and tell us that the following is not thread safe?

对于编译时,理想的情况是在Eclipse中出现一个弯曲的下划线,并告诉我们该类不是线程安全的?

For compile time, ideally in Eclipse the wiggly underline comes up and tells us that the class is not thread safe?

对于运行时,静态代码分析是否会将类视为非线程安全的?

For run time, will any the static code analysis catch the class as non-thread-safe?

public class LazyInitRace {
   private ExpensiveObject instance = null;

    public ExpensiveObject getInstance() {
    if (instance == null)
      instance = new ExpensiveObject();
    return instance;
   }
}

推荐答案

查找错误可以找到您的部分内容线程同步不一致的代码,即,您在一个位置而不是另一个位置同步对字段的访问.它还可以针对 JCIP注释进行基本验证,但我相信只有@Immutable此时已被检查.

FindBugs can find parts of your code where thread synchronization is inconsistent, i.e. you synchronize access to a field in one place but not in another. It can also do basic validation against JCIP annotations, but I believe only @Immutable is checked at the moment.

我不知道有任何一种静态分析工具可以自动捕获这种特殊情况,但是我敢肯定它存在.

I don't know of any static analysis tool that would automatically catch this particular case but I'm sure one exists.

这篇关于线程安全框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆