java Lambda 表达式对于多核系统的优势是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16981796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the advantages of Lambda Expressions for multicore systems?
提问by GedankenNebel
The Java Tutorialsfor Lambda Expressions says following:
Lambda 表达式的Java 教程如下:
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.
本节讨论 Project Lambda 中包含的功能,该项目旨在通过向 Java 语言添加闭包和相关功能来支持多核环境中的编程。
My question is, what concrete advantages do I have with Lambda Expressions according to multicore systems and concurrent/parallel programming?
我的问题是,根据多核系统和并发/并行编程,我使用 Lambda 表达式有什么具体优势?
采纳答案by Brian Agnew
Parallelism is trivial to implement e.g. if you have a collection and you implement a lambda thus:
实现并行是微不足道的,例如,如果你有一个集合并且你实现了一个 lambda,那么:
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.
然后集合本身可以并行化该操作,而无需您自己进行线程处理等。并行性在集合map()
实现中处理。
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 explicitlytake the parallel view on a collectionin order to implement the above.
在纯功能(即无副作用)系统中,您可以对每个 lambda 执行此操作。对于非纯函数环境,您必须选择适用的 lambda(因为您的 lambda 可能无法安全地并行运行)。例如,在Scala中,你必须明确地走在收集平行视图为了实现上述。
回答by Edwin Dalorzo
Some reference material:
一些参考资料:
- You can read Maurice Naftalin's answer in Why are lambda expressions being added to Java.
- Or you can read Mark Reinhold's answer in his article Closures for Java.
- Reinhold also wrote, in his blog, a Closures Q&Awhich seems to address some of your questions.
- And there is even an interesting article in JavaWorld about Understanding the Closures Debate.
- 您可以阅读 Maurice Naftalin 在Why are lambda expressions are added to Java 中的回答。
- 或者您可以阅读 Mark Reinhold 在他的文章Closures for Java 中的回答。
- Reinhold 还在他的博客中写了一个Closures Q&A似乎解决了你的一些问题。
- JavaWorld 中甚至还有一篇关于理解闭包辩论的有趣文章。
回答by Simon
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:
完全尊重 Java 8 lambda 函数和开发人员的意图,我想问:什么是新的,为什么它比传统的接口/方法函数方法更好?为什么它比(假设)更好forEach(IApply)
:
IApply
is interface
IApply
是接口
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.
它如何阻碍并行性?至少 IApply 的实现可以被重用,继承(扩展),在静态类中实现。
在我看到数十个初级错误示例之后,最后一个参数很重要,其中错过了 lambda 代码访问外部类的 this 可能是类在对它的不同引用已经为空后留在内存中的原因。
从这个角度来看,对类的静态成员的引用非常重要(类似于 C#“委托”的情况。并且主要 - 从一方面来看,lambda 是超级封装,从另一个方面来说是不可重用的,同时 - 违反OOD文化的基本原则:免费访问大师会员。从编程文化的角度——逆向上世纪70年代。
函数式编程?-我明白了,但是为什么要混合是 Java 的 OOP 现象。OOD 有一个奇妙的模式叫做数据行为解耦,它优雅地提供了相同的可能性?论点 - 它就像在 Java Script ...nu 中一样,真的!因此,提供将 Java Script 嵌入 Java 并在 Java Script 中编写系统块的工具。所以我仍然没有看到这么多真正的好处,因为有广告浪潮。