Java – a concurrent collection that maintains the insertion order

I am looking for a concurrent list that can maintain the insertion order Does anyone have some good recommendations?

I see some from guava, such as setfrommap, but they are not recommended in the new version

thank you.

Solution

If you mainly do read operations and rarely write operations, and you don't have many elements, you can use copyonwritearraylist, because it is a lock free read operation of list, so it is almost not faster, but it is very expensive. For each write operation, it rebuilds the whole list so that it can provide a new read-only copy for the next read operation

In your case, you have many write operations and many elements to put into the collection. Copyonwritearraylist is obviously not suitable for you

I suggest you use a thread safe queue, which you can use in Java util. Find it in the concurrent package Depending on your context and your JDK version, the best choice may change, but if you don't need to specifically block queues or double ended queues, but only need pure collections, the best choice may be arrayblockingqueue, concurrentlinkedqueue or linkedblockingqueue, but according to this benchmark result (a little old, linkedblockingqueue provides the best overall performance

But when we talk about acting, the first and most important suggestion is: always test the target environment, which is the only effective way to understand what is the best choice

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>