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

查看:21
本文介绍了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 Collections 似乎也没有解决这个特殊问题.但是,有一个项目确实试图解决这个问题.

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天全站免登陆