为 NavigableMap 编写同步的线程安全包装器线程、安全、NavigableMap

2023-09-07 16:43:14 作者:半人半心半凄凉#

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 SetSet extends CollectionCollections 也有SortedSetSet 的专用实用方法.大概 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 Collections doesn't provide utility methods for NavigableMap? How would you write your own synchronized wrapper for NavigableMap? Glancing at the source code for OpenJDK version of Collections.java seems to suggest that this is just a "mechanical" process Is it true that in general you can add synchronized thread-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.util package since you would like to extend static 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 NavigableMap and code them the same way as SynchronizedSortedMap does. Here's ONE example:

        @Override
        public K ceilingKey(K key) {
            synchronized (mutex) { return sm.ceilingKey(key); }
        }

请注意,返回例如 Set 的方法您也需要将其包装在 SynchronizedSet 中.再次,请参阅 SynchronizedMapSynchronizedSortedMap 来源以获得见解:)

Note that the methods which returns for example Set you'll need to wrap it in SynchronizedSet as well. Again, see the SynchronizedMap and SynchronizedSortedMap sources for insights :)

我不认为它(能够)是一个机械过程,因为它涉及很多因素.

I don't expect it to be (able to be) a mechanical process since it involves a lot of factors.

 
精彩推荐
图片推荐