What are the advantages of Lambda Expressions for multicore systems?

12,431

Solution 1

Parallelism is trivial to implement e.g. if you have a collection and you implement a lambda thus:

collection.map { // my lambda }

then the collection itself can parallelise that operation without you having to do the threading etc. yourself. The parallelism is handled within the collection map() implementation.

In a purely functional (i.e. no side effects) system, you can do this for every lambda. For a non-purely functional environment you'd have to select the lambdas for which this would apply (since your lambda may not operate safely in parallel). e.g. in Scala you have to explicitly take the parallel view on a collection in order to implement the above.

Solution 2

Some reference material:

Solution 3

With full respect to Java 8 lambda function and intents o developers I would like to ask: what is the new and why it is better than traditional interface/method function approach? Why it is better than (suppose) forEach(IApply) where:

IApply is interface

public interface IApply {
    void exec(KEY key, VALUE value);
}

How it impedes to parallelism? At least implementation of IApply can be reused, inherited(extended), be implemented in static class.
The last argument is important after dozens of examples of errors of juniors that I seen, which miss that lambda code accesses this of outer class can be a cause of class stays in memory after distinct reference to it is null already.
From this point of view reference to static members of a class are much important (are analogies of a case of C# "delegate"s. And principally- from one hand- lambda is super encapsulation, from another one not reusable, and concurrently - violation of basic principles of OOD culture: free accessing master's members. From point of view of culture of programming- reverse to 70-th years of last century.
Functional programming? -I see, but why to mix is the OOP phenomena that Java is. OOD has wonderful pattern named Data-Behavior decoupling which elegantly provides the same possibilities? The argument - it is same like in Java Script ...nu, really! So give tools to embed Java Script in Java and write chunks of system in Java Script. So I still don't see really so much real benefits, as there are in wave of advertisement.

Share:
12,431
GedankenNebel
Author by

GedankenNebel

Updated on June 09, 2022

Comments

  • GedankenNebel
    GedankenNebel almost 2 years

    The Java Tutorials for Lambda Expressions says following:

    This section discusses features included in Project Lambda, which aims to support programming in a multicore environment by adding closures and related features to the Java language.

    My question is, what concrete advantages do I have with Lambda Expressions according to multicore systems and concurrent/parallel programming?

  • Pragmateek
    Pragmateek almost 11 years
    Just to add to your answer: this ability to parallelize is not specific to lambdas but to functors, ie encapsulation of the processing logic in an object. With the previous versions of Java you could already do that but it was ... clumsy ;) So lambdas are just a great syntactic sugar for writing functors, especially one shot functors.
  • Maurice Naftalin
    Maurice Naftalin almost 11 years
    @Serious: it's somewhat misleading to call lambdas syntactic sugar. They are implemented very differently to anonymous inner classes, which would have been the previous "clumsy" option. Ultimately they will be much more efficient in parallelising (some) code for multicore systems.
  • Pragmateek
    Pragmateek almost 11 years
    Interesting, how are they implemented in Java? Because in C# lambdas are compiled as classes and methods, the only kind of functor a low level platform supports. The closure part is handled via fields. As .Net and the JRE are similar platforms I though it should be similar...
  • Brian Agnew
    Brian Agnew almost 11 years
    Looking at Java 8 (early release), they're implemented as anonymous classes. I'm not sure if that's the final implementation
  • Amir Pashazadeh
    Amir Pashazadeh almost 11 years
    As far as I know (I have read early specs of project Lambda), anonymous inner classes are used in Java too.
  • MohamedSanaulla
    MohamedSanaulla almost 11 years
    I think in Java 8 lambda expressions are compiled down in to class implementations as the functions are not treated as first class citizes or in other words there is no function type in Java. Moreover its not only Lambda expressions helping for multicore systems, but the enhancements of the Collections API to make them use lambda expressions which add to the possible benefit in multicore systems.