2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-05-31 16:20:47 +02:00
|
|
|
Early classes of the Java API, such as `Vector`, `Hashtable` and `StringBuffer`, were synchronized to make them thread-safe.
|
2023-06-06 16:33:23 +02:00
|
|
|
However, synchronization has a significant negative impact on performance, even when using these collections from a single thread.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
|
|
|
|
2023-06-06 16:33:23 +02:00
|
|
|
It is often best to use their non-synchronized counterparts:
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-05-31 16:20:47 +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
|
|
|
|
2023-06-06 16:33:23 +02:00
|
|
|
Even when used in synchronized contexts, you should think twice before using their synchronized counterparts, since their usage can be costly.
|
2023-05-31 16:20:47 +02:00
|
|
|
If you are confident the usage is legitimate, you can safely ignore this warning.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-06-09 16:36:32 +02:00
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
2023-05-31 16:20:47 +02:00
|
|
|
Vector<Cat> cats = new Vector<>();
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-06-09 16:36:32 +02:00
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
2023-05-31 16:20:47 +02:00
|
|
|
ArrayList<Cat> cats = new ArrayList<>();
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Exceptions
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-06-06 16:33:23 +02:00
|
|
|
Usage of these synchronized classes is ignored in the signatures of overriding methods.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
[source,java]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
@Override
|
2023-06-06 16:33:23 +02:00
|
|
|
public Vector getCats() {...} // Compliant
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Replace the synchronized class "{}" by an unsynchronized one such as "{}".
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== 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
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|