SonarQube-Java规范之Atomic-".equals()" should not be used to test the values of "Atomic" classes
来源:互联网 发布:nginx 内置的全局变量 编辑:程序博客网 时间:2024/06/03 14:21
AtomicInteger
, and AtomicLong
extend Number
, but they're distinct from Integer
and Long
and should be handled differently. AtomicInteger
and AtomicLong
are designed to support lock-free, thread-safe programming on single variables. As such, an AtomicInteger
will 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()) { ... }
- SonarQube-Java规范之Atomic-".equals()" should not be used to test the values of "Atomic" classes
- iOS之整型转换警告Values of type 'NSInteger' should not be used as format arguments;
- java编程之Noncompliant; %n should be used in place of \n to produce the platform-specific line separato
- ios学习之路(知识点)Values of type 'NSInteger' should not be used as format arguments...
- When should inner classes be used in Java Interview Question
- Why the "volatile" type class should not be used
- the `gets' function is dangerous and should not be used.
- BigDecimal(double)” should not be used
- java atomic
- Java Atomic
- java atomic
- Java线程之CAS(Atomic)
- Java 多线程安全之 Atomic
- Atomic
- atomic
- Atomic
- the update-modules command is deprecated and should not be used
- warning: the `gets' function is dangerous and should not be used.
- C++ Boost智能指针
- linux 2.6 一个简单的字符驱动程序
- javascript 列表 多选 左右移动
- Python学习2_Python3.x和Python2.x的区别
- mysql 去除重复 Select中DISTINCT关键字的用法
- SonarQube-Java规范之Atomic-".equals()" should not be used to test the values of "Atomic" classes
- gcc for Windows 开发环境介绍
- Ubuntu 下连接iSCSI Volume
- Shell特殊变量:$*和$@的区别
- 遍历Map的两种方法
- gcc for Windows 开发环境介绍
- 在Windows下使用gcc
- Java NIO系列教程(二) Channel
- gnokii发短信,使用配置