Reputation: 31
I have found several off heap in memory data storage, such as Chronicle-Map, mapdb, etc... They all have usage of locks (stamped read write lock, or reentrant read write lock).
Is there any data structure that is off heap and lock free on Java? Or is there any off heap data structure that is read-lock-free?
Upvotes: 3
Views: 1346
Reputation: 1853
Lock-free is different than wait-free. To synchronize producers and consumers of a queue you must wait when the queue is full or empty, there is no escape. And to check for a full/empty queue you can of course use locks. But there is a faster way => memory barriers, which are available in Java through volatile variables, such as the ones from the java.util.concurrent.atomic
package (AtomicLong
, AtomicBoolean
, etc) and through VarHandle
(as it was mentioned in the comments). Besides of course of the volatile keyword.
Off-heap through sun.misc.Unsafe
or the FFM API (java.lang.foreign
) also offers volatile operations as well. It can be used for IPC (across JVMs). Check the explanations of the CoralRing project, which is an open-source implementation of a lock-free off-heap queue.
Disclaimer: I'm one of the developers of CoralRing.
Upvotes: 0
Reputation: 646
Agrona's ring buffer is fully lock free off-heap data structure. This queue could be used as inter-process-communication library (and actually is used inside aeron) and doesn't leverage any locks/system calls for message passing. Besides that, I'm pretty sure Agrona has more off-heap data structures, so you might want to check it out.
Upvotes: 0
Reputation:
Lock-free is not necessarily faster than lock-based.
In modern JVMs, built-in locks are extremely fast, and beat old lock-free algorithms by a high magnitude.
This looks like an XY problem. What are you trying to achieve?
Upvotes: 0