SonarQube-Java规范之Atomic-".equals()" should not be used to test the values of "Atomic" classes
2016-03-30 10:36
666 查看
AtomicInteger, and
AtomicLongextend
Number,
but they're distinct from
Integerand
Longand
should be handled differently.
AtomicIntegerand
AtomicLongare
designed to support lock-free, thread-safe programming on single variables. As such, an
AtomicIntegerwill
only ever be "equal" to itself. Instead, you should
.get()the value and make comparisons
on it.
AtomicInteger类和AtomicLong类继承自Number类,但是它们与Integer类和Long类有不同点,因此在使用上也有不同。AtomicInteger类和AtomicLong类是为支持单个变量的线程安全和无需锁定而设计的。因此,一个AtomicInteger对象的".equals()"方法比较只有在跟自己比较的时候才会返回true。所以,对于比较它们的值是否相等,就应当使用AtomicInteger对象的”.get()“方法,进行取值比较。
This applies to all the atomic, seeming-primitive wrapper classes:
AtomicInteger,
AtomicLong,
and
AtomicBoolean.
这条规则适用于所有的atomic类对象,具体参考封装类:AtomicInteger,AtomicLong和AtomicBoolean
Noncompliant Code Example
错误的代码示例AtomicInteger aInt1 = new AtomicInteger(0); AtomicInteger aInt2 = new AtomicInteger(0); if (aInt1.equals(aInt2)) { ... } // Noncompliant
Compliant Solution
正确的代码示例AtomicInteger aInt1 = new AtomicInteger(0); AtomicInteger aInt2 = new AtomicInteger(0); if (aInt1.get() == aInt2.get()) { ... }
相关文章推荐
- android graphic(18)—How about some Android graphics true facts?
- 设计模式 - Builder模式
- 用marquee和div+js实现首尾相连循环滚动效果
- GTK+ and Glade3 GUI Programming
- gtk+2.0和glade3.4.5的GUI编程
- configure: error: ZLIB extension requires zlib >= 1.0.9
- Druid连接池配置
- UGUI点击事件(委托)
- GUID批量生成
- CMU Sphinx ---Building Language Model(部分翻译)
- 图形界面 Fedora Core 12/Core 11 How to log in GUI as r
- 基于QtQuick2.0开发的诸多漂亮的界面例子(作为入门首选)
- UISegmengedControl - 分段控制器
- 排版布局和UI设计
- context.Request.Files为NULL问题
- 《iOS Human Interface Guidelines》——Segmented Control
- UIViewController 基础篇
- xcodebuild和xcrun自动化编译ipa包 笔记
- Could not find an open hard disk with UUID {57bbe8fe-8b4f-4a0d-aef4-1291b23d2bef}. 返回 代码: VBOX_E_OBJ
- Gradle sync failed: Gradle version 2.2 is required. Current version is 2.10.