- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 Java 中,我经常需要懒惰地获取 ConcurrentMap 的条目,仅在必要时才创建。
例如我可能有
ConcurrentMap<String, AtomicReference<Something>> a = new ConcurrentHashMap<>();
ConcurrentMap<String, Something> b = new ConcurrentHashMap<>();
我想创建一个通用函数来完成这项工作,这样我就不会重复自己 the rather cumbersome double checking code对于每种类型。
以下是我所能得到的:
<K, V, C extends V> V ensureEntry(ConcurrentMap<K, V> map, K key, Class<? super C> clazz) throws Exception {
V result = map.get(key);
if (result == null) {
final V value = (V)clazz.newInstance();
result = map.putIfAbsent(key, value);
if (result == null) {
result = value;
}
}
return result;
}
然后我可以像这样使用它:
AtomicReference<Something> ref = ensureElement(a, "key", AtomicReference.class);
Something something = ensureElement(b, "another key", Something.class);
问题是:该函数很脏,并且仍然有一个不安全的泛型类转换((V)
)。一个完全通用和更清洁的可能吗?也许在 Scala 中?
谢谢!
最佳答案
对于 Java 8 lambda,以下是我能得到的最简单的......
<K, V> V ensureEntry(ConcurrentMap<K, V> map, K key, Supplier<V> factory) {
V result = map.get(key);
if (result == null) {
V value = factory.get();
result = map.putIfAbsent(key, value);
if (result == null) {
result = value;
}
}
return result;
}
ConcurrentMap<String, AtomicReference<Object>> map = new ConcurrentHashMap<>();
ensureEntry(map, "key", () -> new AtomicReference<>());
// or
ensureEntry(map, "key", AtomicReference::new);
关于java - 以原子方式确保 ConcurrentMap 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18568190/
ConcurrentMap 的 JavaDoc - putIfAbsent 说: /** * If the specified key is not already associated
我有这个 ConcurrentMap ConcurrentMap companyLogo = new MapMaker() .concurrencyLevel(
我该如何实现 class CaseInsensitiveConcurrentMap implements ConcurrentMap 就像ConcurrentHashMap 一样工作除了不区分大小写地
根据ConcurrentMap Javadoc : Memory consistency effects: As with other concurrent collections, actions
我有一段代码可以由多个线程执行,该代码需要执行 I/O 绑定(bind)操作,以便初始化存储在 ConcurrentMap 中的共享资源。我需要使这段代码线程安全,并避免不必要的调用来初始化共享资源。
我有一个 ConcurrentHashMap 累积了一些应该定期发送到某处的更改(用于某种复制)。关于更改,只有任何关键事项的最新值。 所以我需要获取并删除一些快照。快照不需要是原子的,只要它的每个条
ConcurrentMap 指定putIfAbsent() 的返回值为: the previous value associated with the specified key, or null i
我有一个有趣的问题需要帮助。我已经为两个不同的条件实现了几个队列,一个基于 FIFO,另一个基于键的自然顺序(ConcurrentMap)。也就是说,您可以想象两个队列具有相同的数据,只是顺序不同。我
在 Java 中,我经常需要懒惰地获取 ConcurrentMap 的条目,仅在必要时才创建。 例如我可能有 ConcurrentMap> a = new ConcurrentHashMap<>();
我经常想要访问(并且可能添加/删除)给定 ConcurrentMap 的元素,以便一次只有一个线程可以访问任何单个键。做这个的最好方式是什么?同步 key 本身不起作用:其他线程可能通过 equal
休息服务有许多彼此不相关的客户。每个客户都可以随时开始投票过程。一旦客户添加了第一票,我就会在 中创建一个条目 ConcurrentMap> votesByCustomer; 每个投票都有 voteT
我有以下代码。即使集合本身不是线程安全的,它也会是线程安全的吗? private ConcurrentMap> proxyRoles = new ConcurrentHashMap>(); p
使用 Java 8,我在 java.util.concurrent.ConcurrentMap 上实现了一个包装器接口(interface),特别是 remove(Object key)方法。由于我需
我有一段代码可以由多个线程执行,这些线程需要执行 I/O 绑定(bind)操作以初始化存储在 ConcurrentMap 中的共享资源。我需要使此代码线程安全并避免不必要的调用来初始化共享资源。这是错
AbstractConcurrentMap是Groovy中的核心类,它用于存储在运行时添加到Groovy类中的动态属性。我正在使用 Grails 2.1.2 和 Groovy 1.8.8,但我认为所有
我正在声明一个 MultiMap,并想知道将键设置为 Integer 是否是一个好主意。使用整数键与字符串键会加快检索值的速度吗? MultiMap mmCustomerOrders = hz.get
在 Java 的 ConcurrentMap 中, 有 remove(key, expectedValue) ,这将返回以下之一: 预期值在那里,但已被删除。 预期值不存在,因此未被删除。 但我想要得
很多人将 ConcurrentMap 称为缓存。 这样做是个好主意吗: public List listTasks(final ProcessDefinition def, final boolean
我正在尝试反序列化其中包含 ConcurrentMap 的对象,但出现异常。 Caused by: java.lang.IllegalArgumentException: Can not set ja
我有一个java类来处理多线程订阅服务。通过实现 Subscribable 接口(interface),可以将任务提交到服务并定期执行。代码草图如下所示: import java.util.concu
我是一名优秀的程序员,十分优秀!