Thread Safe Interprocess Shared Memory in Java (in 7 mins)
1. Thread Safe Interprocess
Shared Memory in Java
(in 7 minutes)
Peter Lawrey
Principal Consultant
Higher Frequency Trading
January 2014
Thread Safe Interprocess Shared Memory in
Java
1
2. Shared Memory
ïŹ
ïŹ
ïŹ
ïŹ
Java naturally uses memory on the heap
(and stack)
Java can also access memory mapped files
These files can be accessed and shared by
multiple processes at once
They can be persisted, or stored on a tmpfs or
RAM drive
January 2014
Thread Safe Interprocess Shared Memory in Java
2
3. Thread Safe
ïŹ
ïŹ
Java's thread safe constructs are all on the
heap
You can use Unsafe and create off heap
thread safe constructs such as memory
barriers, volatile access and CAS operations
January 2014
Thread Safe Interprocess Shared Memory in Java
3
4. What is CAS?
ïŹ
ïŹ
ïŹ
CAS is Compare And Swap also known as
Compare And Set in some Java libraries
It is a thread safe operation which only
succeeds for one thread at a time and can be
used to build a lock
Is a single machine code instruction â fast
January 2014
Thread Safe Interprocess Shared Memory in Java
4
5. Why not use the heap?
ïŹ
Can support 100s of GB with no GC impact
ïŹ
Can share data structures between processes
ïŹ
Can persist data without extra overhead
This allows you to implement a simple
embedded database with sub micro-second
read/write latency
January 2014
Thread Safe Interprocess Shared Memory in Java
5
6. sun.misc.Unsafe
public native compareAndSetInt(
Object obj,
// can be null
long offset,
int expected,
int value);
Also compareAndSetLong
Likely to go away in Java 9
January 2014
Thread Safe Interprocess Shared Memory in Java
6
7. Bytes
OpenHFT/Java-Lang has a wrapper
ïŹ
64-bit replacement for ByteBuffer
ïŹ
Thread safe operations
ïŹ
Supports binary data, text parsing and object
serialization. e.g. ObjectInput/Ouput
January 2014
Thread Safe Interprocess Shared Memory in Java
7
8. Demo - LockingViaMMapMain
Two processes toggle flags in many locked records
One process flips false â true
The other flips true â false
Toggled 100,000,000 times with
an average delay of 49 ns
January 2014
Thread Safe Interprocess Shared Memory in
Java
8