The exact operations used should determine what is used. However, since the container is basically an abstract type, write it so that it works - reliably - then a profile, provides functional requirements, optimizes as necessary, blah blah :)
Usually my main use of the "simultaneous collection" is the queue, which is used to transfer objects between threads. In this case, I start with ConcurrentLinkedQueue , if for some other reason than I like the "blocking" algorithm (this does not mean that it will be faster :-).
Typically, a queue and / or LinkedList is a good data structure to add to the end. Depending on the circumstances, including specific usage patterns, including: thread conflict, number of deleted elements, deleting elements, etc., "Quick destruction" of all but the beginning and the end can be done faster with clear (part of AbstractQueue) and re-adding elements - ConcurrentLinkedQueue allows you to control and manipulate the head and tail. However, I would like to ask for “simplify”, write “a specific interface contract” and “just use the current approach” until it is convincingly proved that the functional requirement is not fulfilled.
In terms of performance, it really depends on the specific characteristics of the data / operations structure and the data structure and data structure. In some cases, an ArrayList can be slower than Vector, and is really documented. If this performance matters, then something sounds suspicious. An article in Java's Best Practices - Vector vs ArrayList vs HashSet contains a pleasant read. Pay attention also to the comments.
Edit: Remember that a “parallel” data structure usually implies that one operation (method call) is atomic (and perhaps not all operations in odd cases!). It may be necessary to perform synchronization with a wide range or another approach to achieve the required level of thread safety. That is, h.putIfAbsent(k,v) (from ConcurrentHashMap) does not match if (!h.containsKey(k)) { h.put(k, v); } if (!h.containsKey(k)) { h.put(k, v); } - like case-in-point, this problem refers to the "clear then add" approach mentioned above.
Happy coding.
user166390
source share