Thread Safe Interprocess Shared Memory in Java (in 7 mins)

9,516 views
8,882 views

Published on

A lightening talk on using Shared Memory in Java is a Thread Safe manner and why you would want to.

Published in: Technology, News & Politics
3 Comments
11 Likes
Statistics
Notes
  • Thanks for the many great presentations on memory mapped IO with Java. A few questions on this. 1) Is there a link to the source of toggle demo? 2.) If two processes map the same file is there anything special we need to do to make sure changes from process 0 will be visible to process 1 immediately?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Good point. I confused this with AtomicLong.compareAndSet()

    The plan is to remove Unsafe completely and it has been suggested that there be a replacement. One is proposed here https://groups.google.com/forum/#!forum/jep-off-heap

    As yet there isn't any work around except using JNI.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Peter,
    I couldnt find compareAndSetXXX() function in Unsafe source code? Did you mean compareAndSwapXXX()?

    Why do you think they are removing it in JAVA 9 and what could happen to applications using it? Any work arounds?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
9,516
On SlideShare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
81
Comments
3
Likes
11
Embeds 0
No embeds

No notes for slide

Thread Safe Interprocess Shared Memory in Java (in 7 mins)

  1. 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. 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. 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. 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. 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. 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. 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. 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
  9. 9. 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
  10. 10. 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

×