Why is there no concurrent ArrayList implementation in the java.util.concurrent package?

Why is there no concurrent ArrayList implementation in the java.util.concurrent package?

 

Q: JDK 5 introduced ConcurrentHashMap in java.util.concurrent. In scenarios that need to support high concurrency, we can use it instead of HashMap. But why is there no concurrent implementation of ArrayList? Do we only have a thread-safe array implementation like Vector to choose from in a multi-threaded scenario? Why isn't there a class in java.util.concurrent that can replace Vector?

 

Answer: I think the main reason for not adding a concurrent ArrayList implementation in the java.util.concurrent package is that it is difficult to develop a generic and thread-safe List without concurrency bottlenecks.

 

The real point / value of classes like ConcurrentHashMap is not that they guarantee thread safety. Rather, they do not have concurrency bottlenecks while ensuring thread safety. For example, ConcurrentHashMap uses lock segmentation technology and weakly consistent Map iterators to avoid concurrency bottlenecks.

 

So the problem is, with data structures like "Array List", you don't know how to circumvent the concurrency bottleneck. Take an operation like contains() , how do you avoid locking the entire list when you search?

 

On the other hand, Queue and Deque (based on Linked List) have concurrent implementations because their interfaces have more restrictions than List's interfaces, and these restrictions make it possible to implement concurrency.

 

CopyOnWriteArrayList is an interesting example that circumvents the concurrency bottleneck of read-only operations (like get/contains ), but it does a lot of work in modifying operations and modifying the visibility rules to do so. In addition, the modification operation also locks the entire List, so this is also a concurrency bottleneck. So theoretically, CopyOnWriteArrayList is not a general concurrent List.

Reprinted from Concurrent Programming Network – ifeve.com link to this article: Why is there no concurrent ArrayList implementation in the java.util.concurrent package?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326290714&siteId=291194637