rspec/rules/S1149/java/rule.adoc

69 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
Early classes of the Java API, such as `Vector`, `Hashtable` and `StringBuffer`, were synchronized to make them thread-safe.
However, synchronization has a big negative impact on performance, even when using these collections from a single thread.
2021-04-28 16:49:39 +02:00
It is often best to use their new non-synchronized counterparts:
2021-04-28 16:49:39 +02:00
* `ArrayList` or `LinkedList` instead of `Vector`
* `Deque` instead of `Stack`
* `HashMap` instead of `Hashtable`
* `StringBuilder` instead of `StringBuffer`
2021-04-28 16:49:39 +02:00
Even when used in synchronized context, you should think twice before using their synchronized counterparts, since their usage can be costly.
If you are confident the usage is legitimate, you can safely ignore this warning.
2021-04-28 16:49:39 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
Vector<Cat> cats = new Vector<>();
2021-04-28 16:49:39 +02:00
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
ArrayList<Cat> cats = new ArrayList<>();
2021-04-28 16:49:39 +02:00
----
=== Exceptions
2021-04-28 16:49:39 +02:00
Use of those synchronized classes is ignored in the signatures of overriding methods.
[source,java]
2021-04-28 16:49:39 +02:00
----
@Override
public Vector getCats() {...}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace the synchronized class "{}" by an unsynchronized one such as "{}".
'''
== Comments And Links
(visible only on this page)
=== relates to: S1076
=== on 31 Jul 2013, 15:13:51 Dinesh Bolkensteyn wrote:
Can also replace UseArrayListInsteadOfVector
=== on 31 Jul 2013, 15:18:08 Dinesh Bolkensteyn wrote:
Implemented by \http://jira.codehaus.org/browse/SONARJAVA-236
endif::env-github,rspecator-view[]