为NavigableMap编写一个同步的线程安全包装器 [英] Writing a synchronized thread-safety wrapper for NavigableMap

查看:146
本文介绍了为NavigableMap编写一个同步的线程安全包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.util .Collections 目前提供了以下用于为各种收集接口创建同步包装器的实用程序方法:





类似地,它也有6个未修改的XXX 重载。



NavigableMap< K, V> 。它的确是扩展SortedMap ,但是 SortedSet extends Set Set extends Collection 和集合 SortedSet 的专用实用方法。推测 NavigableMap 是一个有用的抽象,否则它不会在那里首先,但没有实用的方法。



所以问题是:




  • 有什么具体原因 / code>不提供 NavigableMap
  • 的实用程序方法
  • 如何编写自己的 synchronized 包装器 NavigableMap


    • 浏览 OpenJDK的 Collections.java 的源代码似乎暗示这只是一个机械过程


      • 这是真的,一般来说,你可以添加 synchronized thread-safetiness功能这样吗?

      • 如果这是一个机械过程,可以自动化吗? (Eclipse插件等)

      • 此代码是否需要重复使用,或者可以通过不同的OOP设计模式避免?




解决方案

修正正在进行


Josh写道:



他们绝对属于那里,他们的缺席是无意的。



我同意,即使我们的工程师都没有期待
写作(和测试)所有的心灵转移的方法。
发布日期:2006-08-21 00:50:41.0




更新:关于手动实现它,您可能会考虑劫持 java.util 你想扩展 static class SynchronizedSortedMap< K,V> ,这是声明的包的私有。否则它将是很多代码copypaste。这是一个启动:

  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>扩展SynchronizedSortedMap< K,V>实现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 相同的方式对它们进行编码。这里有一个例子:

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

注意,返回的方法例如 Set ,您需要将它包装在 SynchronizedSet 中。同样,请参阅 SynchronizedMap SynchronizedSortedMap 资源:)



我不希望它是(能够)机械过程,因为它涉及很多因素。


java.util.Collections currently provide the following utility methods for creating synchronized wrapper for various collection interfaces:

Analogously, it also has 6 unmodifiedXXX overloads.

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.

So the questions are:

  • 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?

解决方案

This was an oversight. The fix is in progress.

Josh writes:

"They definitely belong there. Their absence is unintentional.
We should put them in as soon as possible."

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

It takes a time though.

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;
        }

    }
}

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); }
        }

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.

这篇关于为NavigableMap编写一个同步的线程安全包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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