Parallel-safe lambdas

Stephen Colebourne scolebourne at joda.org
Wed Feb 10 18:23:19 PST 2010


Recent threads have suggested that Project Lambda is primarily (ie
very) interested in the parallel execution use case (specifically
Parallel Array).

The problem has arisen that exposing "this" and local variables can
cause race conditions. As such, I thought I'd outline a way to avoid
that:

Example, filter a large list in parallel based on a predicate that
uses a local (or instance) variable. It filters strings of length 0,
2, 3 and 5 (yes, its a stupid example):

 boolean[] lengthAcceptable = new boolean[] {true, false, true, true,
false, true};
 ParallelArrays.filter(list, #(String str : boolean[] lengths =
lengthsAcceptable) {
   if (str.length() > lengths.length) {
     return true;
   }
   return lengths[str.length()];
 });

Rules (not formal JLS):
- "this" is scoped to the lambda and provides no access to the lexical
scope (or anything other than the lambda object)
- variables must be 'imported' into the lambda scope via manual
capture - a colon followed by variable declaration style statements -
the ": boolean[] lengths = lengthsAcceptable" part in the example
- variables imported in this manner are deep-cloned or serialized to
create a fully independent copy to be used by the lambda
- variables imported are copied at lambda creation time - there is no
'capture' of the variable
- attempts to use the lexical scope ("this", instance variables, local
variables) do not compile
- if access to the lexical "this" is required, then it must be
manually captured, which will cause it to be deep cloned

With these rules, I believe (its late here) that a lambda cannot
suffer a data race. Therefore, I claim that this solution correctly
meets the "primary goal" of parallel execution and parallel arrays.

Of course, this is actually an ugly, excessively boilerplate, overly
protective approach that is extremely use-case driven and would be a
terrible addition to the Java language. But it would seem a reasonably
natural conclusion of the "primary goal" of parallel execution
argument.

Sometimes its instructive to see what the full-blown solution might
look like before arriving at a suitable middle ground. As I hope is
plain, I believe the middle ground involves lexically-scoped "this",
amongst other things.

Stephen


More information about the lambda-dev mailing list