26. Off-heap Collections
● Not limited to Heap capacity
● Not limited to Integer.MAX_VALUE size
● No boundary checks
● Not under GC management
● Partially available at java.nio.*
● Useful for math computations
● Realtime programming
“The price of greatness is responsibility.”
– Winston Churchill
38. Concurrency: CAS Counter
CAS = Compare And Swap
Counter result: 100000000
Time passed in ms: 6454
39. Lock-free Data Structures
Intuition
● Create a copy of current state
● Modify it
● CAS
● Repeat if it fails
Problems
● Hard to implement.
● Problem ABA (DCAS, LL/SC)
● Reordering (Memory Barriers)
41. Unsafe.park()
“Block current thread, returning when a balancing
unpark occurs, or a balancing unpark has already
occurred, or the thread is interrupted, or, if not
absolute and time is not zero, the given time
nanoseconds have elapsed, or if absolute, the given
deadline in milliseconds since Epoch has passed, or
spuriously (i.e., returning for no "reason"). Note:
This operation is in the Unsafe class only because
unpark is, so it would be strange to place it
elsewhere.”
– Javadoc
42. Disclaimer
● May be removed/changed
● Not portable
● Depends on architecture
● Depends on JVM implementation
● Depends on major/minor JVM
● Never use it in production
Gentle reminder:
One error may cause
JVM crash
44. Links
● Javadoc http://www.docjar.com/docs/api/sun/misc/Unsafe.html
● My Blog http://mishadoff.github.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/
● Tricks with Direct Memory Access
http://highlyscalable.wordpress.com/2012/02/02/direct-memory-access-in-java/
● StackOverflow http://stackoverflow.com/questions/5574241/interesting-uses-of-sun-misc-unsafe
● Native C/C++ Like Performance For Java Object Serialisation
http://mechanical-sympathy.blogspot.de/2012/07/native-cc-like-performance-for-java.html
● Big Arrays in Java http://www.omsn.de/blog/big-arrays-in-java
● Lock-Free Wait-Free Hash Table
http://www.azulsystems.com/about_us/presentations/lock-free-hash
● Java theory and practice: Going atomic
www.ibm.com/developerworks/java/library/j-jtp11234/
● Fast multipurpose serialization (RU) http://www.javaspecialist.ru/2012/07/blog-post.html