From 49f101d1862bc550d8b0114e50bafd0045b646db Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Sun, 12 Feb 2012 19:20:00 +0100 Subject: [PATCH] #1813 - Adding clarification regarding JMM and Futures --- akka-docs/general/jmm.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/akka-docs/general/jmm.rst b/akka-docs/general/jmm.rst index 7d806f9ac8..3fe94d89db 100644 --- a/akka-docs/general/jmm.rst +++ b/akka-docs/general/jmm.rst @@ -45,6 +45,18 @@ To prevent visibility and reordering problems on actors, Akka guarantees the fol Both rules only apply for the same actor instance and are not valid if different actors are used. +Futures and the Java Memory Model +--------------------------------- + +The completion of a Future "happens before" the invocation of any callbacks registered to it are executed. + +We recommend not to close over non-final fields (final in Java and val in Scala), and if you *do* choose to close over +non-final fields, they must be marked *volatile* in order for the current value of the field to be visible to the callback. + +If you close over a reference, you must also ensure that the instance that is referred to is thread safe. +We highly recommend staying away from objects that use locking, since it can introduce performance problems and in the worst case, deadlocks. +Such are the perils of synchronized. + STM and the Java Memory Model ----------------------------- Akka's Software Transactional Memory (STM) also provides a "happens before" rule: