java有一个“LinkedConcurrentHashMap”数据结构? [英] Does java have a "LinkedConcurrentHashMap" data structure?

查看:388
本文介绍了java有一个“LinkedConcurrentHashMap”数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个LinkedHashMap的数据结构,并且线程安全。

I need a data structure that is a LinkedHashMap and is thread safe.

我该怎么做?

推荐答案

您可以将地图包装在Collections.synchronizedMap中,以获取维护插入顺序的同步哈希表。这不如ConcurrentHashMap那么有效(并且不实现ConcurrentMap的额外的接口方法),但它确实会让你有线程安全的行为。

You can wrap the map in a Collections.synchronizedMap to get a synchronized hashmap that maintains insertion order. This is not as efficient as a ConcurrentHashMap (and doesn't implement the extra interface methods of ConcurrentMap) but it does get you the (somewhat) thread safe behavior.

甚至强大的Google收藏夹似乎还没有解决这个特殊问题。但是,一个项目尝试解决问题。

Even the mighty Google Collections doesn't appear to have solved this particular problem yet. However, there is one project that does try to tackle the problem.

我在某种程度上说同步,因为迭代仍然不是线程安全的,因为并发修改异常可能会发生。

I say somewhat on the synchronization, because iteration is still not thread safe in the sense that concurrent modification exceptions can happen.

这篇关于java有一个“LinkedConcurrentHashMap”数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆