Best Java thread blocking mechanism for collections?

What would be the slowest thread-safe mechanism for managing multiple collection calls in Java?

I am adding objects to the top of the collection and I am very unsure what will be the most efficient collection. Will it be a vector or a queue? Initially, I thought ArrayList would be fast, but I did some experiments, and it was very slow.

EDIT: In my testing, inserting Vector delared using volatile seems the fastest?

+4
source share
6 answers

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.

+4
source

See collections in java.util.concurrent .

+6
source

The best algorithms prevent sharing as much as possible. It sounds like you are new to multi-threaded programming in java. Why not just choose what you know will be right ... Then see how much synchronization exists, and then see if this is a problem ... then see if you can find a faster way to do this.

+5
source

Perhaps write your own implementation of LinkedList?

The java collection contains only the current pointer. You can have one that holds its head and tail ...

You can also wrap it inside the Collections.synchronizedList (...) collection.

0
source

If I understand the comment "Vector using volatile" correctly, you may not quite understand what synchronization is. Volatile only apply to a link to Vector; and since the vector itself is synchronized, it will be redundant.

But basically, do you really think that performance differences due to synchronization (for access to containers) are enough for you to worry? If you do this, you can see the effect with profiling (hotspots in access and / or mutation). I am not saying that there are no cases where this can happen, but they are not particularly common.

0
source

ConcurrentSkipListMap / Set - but you should know how / WHEN to use it. CopyOnWriteArrayList is another great solution (again, you need to know why you use it).

EDIT: In my testing, inserting Vector delared using volatile seems the fastest?

It is simply incomprehensible and completely not useful.

0
source

Source: https://habr.com/ru/post/1335634/


All Articles