2

Fastutil seems to be the fastest option for collections in Java.

There are these javadocs: http://fastutil.di.unimi.it/docs/it/unimi/dsi/fastutil/longs/Long2ObjectMaps.SynchronizedMap.html

but I have difficulty to find the usage example.

How can I have a synchronized (thread-safe?) map? An immutable map?

onkami
  • 8,791
  • 17
  • 90
  • 176

2 Answers2

1

Each map type has its own synchronized wrapper class (e.g. Long2ObjectMaps.SynchronizedMap as you linked above). To use it, instantiate a concrete map implementation, then use the corresponding …Maps.synchronize() method to get the synchronized version:

Long2ObjectMap<Foo> unsynched = new Long2ObjectOpenHashMap<>();

// this is actually a Long2ObjectMaps.SynchronizedMap
Long2ObjectMap<Foo> synched = Long2ObjectMaps.synchronize(unsynchronized);
David Moles
  • 48,006
  • 27
  • 136
  • 235
0

There is a lightweight thread-safe alternative: FastUtil Concurrent Wrapper .

Compared to the default fastutil synchronization approach via synchronized(mutex) The Wrapper uses striped ReadWriteLocks (with different modes) which have better performance in some cases.

mchernyakov
  • 196
  • 3
  • 14