Thread Safe Interprocess Shared Memory in Java (in 7 mins)
-
Upload
peter-lawrey -
Category
Technology
-
view
5.537 -
download
1
description
Transcript of Thread Safe Interprocess Shared Memory in Java (in 7 mins)
Thread Safe InterprocessShared Memory in Java
(in 7 minutes)
Peter LawreyPrincipal Consultant
Higher Frequency Trading
January 2014 Thread Safe Interprocess Shared Memory in Java
1
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
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
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
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
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
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
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
Questions?
@PeterLawrey
github.com/OpenHFT
Thank you to
SkillsMatter – the host
RecWorks – the organisers
LJC – London Java Community
January 2014 Thread Safe Interprocess Shared Memory in Java
9