GedankenNebel
GedankenNebel

Reputation: 2587

What are the advantages of Lambda Expressions for multicore systems?

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?

Upvotes: 10

Views: 12558

Answers (3)

Simon
Simon

Reputation: 140

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.

Upvotes: 2

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78589

Some reference material:

Upvotes: 7

Brian Agnew
Brian Agnew

Reputation: 272297

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.

Upvotes: 8

Related Questions