Writing a synchronized thread-safety wrapper for NavigableMap(为 NavigableMap 编写同步的线程安全包装器)
问题描述
java.util.Collections 目前提供以下实用方法来为各种集合接口创建 synchronized 包装器:
synchronizedCollection(Collection<T>c)synchronizedList(Listlist) synchronizedMap(Mapm) synchronizedSet(Sets) synchronizedSortedMap(SortedMapm) synchronizedSortedSet(SortedSets)
类似地,它也有 6 个 unmodifiedXXX 重载.
Analogously, it also has 6 unmodifiedXXX overloads.
这里明显的遗漏是 的实用方法NavigableMap.extends SortedMap 确实如此,但 SortedSet extends Set 和 Set extends Collection 和 Collections 也有SortedSet 和 Set 的专用实用方法.大概 NavigableMap 是一个有用的抽象,否则它一开始就不会存在,但它没有实用方法.
The glaring omission here are the utility methods for NavigableMap<K,V>. It's true that it extends SortedMap, but so does SortedSet extends Set, and Set extends Collection, and Collections have dedicated utility methods for SortedSet and Set. Presumably NavigableMap is a useful abstraction, or else it wouldn't have been there in the first place, and yet there are no utility methods for it.
所以问题是:
Collections没有为NavigableMap提供实用方法有什么具体原因吗?- 您将如何为
NavigableMap编写自己的synchronized包装器?- 浏览
Collections.java的 OpenJDK 版本的源代码似乎表明这只是一个机械"过程- 是不是一般可以像这样添加
synchronized线程安全特性? - 如果是这样一个机械过程,能否实现自动化?(Eclipse 插件等)
- 这种代码重复是必要的,还是可以通过不同的 OOP 设计模式避免?
- Is there a specific reason why
Collectionsdoesn't provide utility methods forNavigableMap? - How would you write your own
synchronizedwrapper forNavigableMap?- Glancing at the source code for OpenJDK version of
Collections.javaseems to suggest that this is just a "mechanical" process- Is it true that in general you can add
synchronizedthread-safetiness feature like this? - If it's such a mechanical process, can it be automated? (Eclipse plug-in, etc)
- Is this code repetition necessary, or could it have been avoided by a different OOP design pattern?
推荐答案
这是一个疏忽.修复正在进行中.
乔希写道:
他们绝对属于那里.他们的缺席是无意的.
我们应该尽快把它们放进去.""They definitely belong there. Their absence is unintentional.
We should put them in as soon as possible."我同意,尽管我们没有一个工程师期待编写(和测试)所有那些令人麻木的转发方法.发布日期:2006-08-21 00:50:41.0
I agree, even though none of us engineers are looking forward to writing (and testing) all those mind-numbing forwarding methods. Posted Date : 2006-08-21 00:50:41.0
这需要一些时间.
更新:至于手动实现,你可以考虑劫持
java.util包,因为你想扩展static class SynchronizedSortedMap被声明为包私有.否则它将是大量的代码复制粘贴.下面是开场白:Update: as to manually implementing it, you may consider to hijack the
java.utilpackage since you would like to extendstatic class SynchronizedSortedMap<K, V>which is declared package private. Else it's going to be a lot of code copypaste. Here's a kickoff:package java.util; import java.util.Collections.SynchronizedSortedMap; public class NewCollections { public static <K, V> NavigableMap<K, V> synchronizedNavigableMap(NavigableMap<K, V> m) { return new SynchronizedNavigableMap<K, V>(m); } static class SynchronizedNavigableMap<K, V> extends SynchronizedSortedMap<K, V> implements NavigableMap<K, V> { private final NavigableMap<K, V> sm; SynchronizedNavigableMap(NavigableMap<K, V> m) { super(m); sm = m; } SynchronizedNavigableMap(NavigableMap<K, V> m, Object mutex) { super(m, mutex); sm = m; } } }让 IDE 自动生成
NavigableMap的未实现方法,并以与SynchronizedSortedMap相同的方式对它们进行编码.这是一个例子:Let the IDE autogenerate the unimplemented methods of
NavigableMapand code them the same way asSynchronizedSortedMapdoes. Here's ONE example:@Override public K ceilingKey(K key) { synchronized (mutex) { return sm.ceilingKey(key); } }请注意,返回例如
Set的方法您也需要将其包装在SynchronizedSet中.再次,请参阅SynchronizedMap和SynchronizedSortedMap来源以获得见解:)Note that the methods which returns for example
Setyou'll need to wrap it inSynchronizedSetas well. Again, see theSynchronizedMapandSynchronizedSortedMapsources for insights :)我不认为它(能够)是一个机械过程,因为它涉及很多因素.
I don't expect it to be (able to be) a mechanical process since it involves a lot of factors.
这篇关于为 NavigableMap 编写同步的线程安全包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
- Is it true that in general you can add
- Glancing at the source code for OpenJDK version of
- 是不是一般可以像这样添加
- 浏览
本文标题为:为 NavigableMap 编写同步的线程安全包装器
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01
