Writing a synchronized wrapper for thread safety for NavigableMap

java.util.Collectionscurrently provide the following utility methods for creating wrappers synchronizedfor various collection interfaces:

Similarly, it also has 6 unmodifiedXXXoverloads.

An impressive omission here is the utilities for NavigableMap<K,V>. It is true that it is extends SortedMap, but also SortedSet extends Setand Set extends Collection, and Collectionshave special tools for methods SortedSetand Set. Presumably NavigableMapa useful abstraction, otherwise he would not be there in the first place, and yet there are no utility methods for him.

So the questions are:

  • Is there a specific reason why Collectionsutility methods do not provide for NavigableMap?
  • synchronized NavigableMap?
+2
4

. .

:

" . .
".

, ( ) . : 2006-08-21 00: 50: 41.0

.

. , java.util, static class SynchronizedSortedMap<K, V>, . . :

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. :

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

, , , , Set, SynchronizedSet. , SynchronizedMap SynchronizedSortedMap :)

, ( ) , .

+4

, ?

, . (IMO),

, ( ), - .

, , "-" , ? , - , ; !

, , EJB. EJB ( ). EJB : " ".

public class MakeSomethingThreadSafe implements Something {
    private final Object lock = new Object();
    private final Something subject;

    public MakeSomethingThreadSafe(Something subject){
        this.subject = subject;
    }

    public void someMethod(){
        synchronized(lock){
            subject.someMethod();
        }
    }
}

- ?

:

, NavigableMap?

.

NavigableMap?

.

, ? , ? ( ..)

, , .

  • 2 ; , .
  • .
  • .

?

Set, Map ..? "" .

+2

, Collections NavigableMap?

.

  • ,
  • " - "
  • , , .

. , , .

, , ?

. , , , , .

, ? ( Eclipse ..)

... . .

?

, . -. .

+1
source

FYI, Java 8 now has this:

private NavigableMap<Date,T> map = Collections.synchronizedNavigableMap(...);

See Java8 doc on navigation chart

+1
source

All Articles