分享

为什么java.util.concurrent包没有concurrentList?

 hh3755 2014-08-07

Why is there not Concurrent ArrayList in java.util.concurrent package.

I think that the main reason is that is difficult to understand how you could code a general thread-safe array list(*) that doesn't have inherent concurrency bottlenecks.

The real point / value of classes like ConcurrentHashMap is not that they are thread-safe. It is that they are thread-safe without being a concurrency bottleneck. For instance ConcurrentHashMap avoids the bottlenecks by having different locks for different parts of the table, and by relaxing the semantics of the map iterators.

The problem with the "array list" family of data structures is that it not obvious how you can avoid the bottlenecks. Take the contains() operation for example. How can you avoid locking the entire list while you search for the element?

The other observation is that the Queue and Deque interfaces do have concurrent implementations (based on linked lists). These APIs are more limited than the List API, but it is the limitations that makes concurrent implementations possible; c.f. the List.contains(...) problem.


(* The CopyOnWriteArrayList class is an interesting case. It avoids the concurrency bottleneck on read-only operations such as get and contains, but it does this by doing a lot more work in mutating operations, and by modifying the visibility rules. Furthermore, the mutating operations lock the entire list, and are therefore a concurrency bottleneck. These properties mean that CopyOnWriteArrayList can't be called a general purpose concurrent list.)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多