Author:Joseph D. Darcy
Organization:Oracle
Owner:Brian Goetz
Created:2011/11/1
Updated:2013/2/21
Type:Feature
State:Funded
Component:–/–
Scope:SE
JSR:269 MR, 335
Discussion:lambda dash dev at openjdk dot java dot net
Start:2011/Q4
Blocks:107, 109
Effort:XL
Duration:XL
Reviewed-by:Brian Goetz
Endorsed-by:Brian Goetz
Funded-by:Oracle
Release 8
Target M7
Summary
Add lambda expressions (closures) and supporting features, including method references, enhanced type inference, and virtual extension methods, to the Java programming language and platform.java
摘要程序員
Java 8添加了Lambda表達式(閉包)和特性支持,包括方法的引用,加強類型推斷,和虛擬擴展方法。算法
Goalsexpress
The primary features of lambda expressions and virtual extension methods, along with their set of secondary supporting features, further several platform goals:編程
Simplifying the creation and consumption of more abstract, higher-performance libraries
Supporting smoother library evolution with migration compatibility
Besides adding a now-common feature to the Java programming language, lambda expressions open up possibilities for improved multicore support by enabling internal iteration idioms.數組
The supporting language features around lambda include virtual extension methods, which will allow interfaces to be evolved in a source and binary compatible fashion.數據結構
In addition to language changes, coordinated libraries and JVM changes will occur as well.閉包
Note that the active and ongoing Project Lambda OpenJDK project pre-dates the JEP process, as does the corresponding JSR, JSR 335, which is targeted for Java SE 8 (JSR 336).併發
Lambda表達式和虛擬擴展方法的主要特色,以及對於Java語言的一整套輔助支持特性,起到促進一些平臺的目的:app
·使得更加抽象,性能更高的庫建立和消費起來更簡單 ·支持平滑的演進庫與兼容性遷移
Lamdba除了增長一個現有經常使用的特色外,還開創的經過使內部迭代語句支持了多核的特性。
圍繞lambda的語言功能支持包括虛擬擴展方法,這將使接口的源代碼和二進制兼容的方式演變升級。
除了語言的變化以外,協調庫和JVM的變化也會變得更加友好。
須要注意的是,積極和持續的Project Lambda OpenJDK項目的預期,JEP的過程,與相應的JSR,JSR335同樣,只是針對用於Java SE8(JSR336)。
Non-Goals
The language features of function types and general control abstraction are not goals of adding lambda expressions to Java. However, the intention is to not preclude the addition of such features in the future.
函數類型和一般的控制抽象等語言特色並非添加lambda表達式到Java的目標,然而,意圖是不排除在未來添加這些特徵。
Motivation
Many other commonly used object-oriented programming languages, including those hosted on the JVM (for example Groovy, Ruby, and Scala) and hosted on other virtual machines (C# on the CLR), include support for closures. Therefore, Java programmers are increasingly familiar with the language feature and the programming models it enables.
Of particular interest is enabling the idiom of internal iteration. Arrays and collections currently support external iteration, where the control logic of an iteration lives outside of the data structure being traversed. For example, a for-each loop over an array or a collection is an example of external iteration. The semantics of the for loop in Java mandates strict serial iteration, which means that the only means the programmer has of iterating over a standard collection will not allow the use of all the available cores.
With internal iteration, the data structure is passed a piece of code to execute, as a lambda expression, and the data structure is responsible for partitioning the computation and reporting the results. Since the data structure is familiar with its own internal details it can potentially choose a better scheduling of the computation by tuning options such as
Alternate execution order
Concurrent execution using thread pools
Parallel execution using partitioning and work stealing. The fork/join framework, added in Java SE 7, is one such candidate parallel execution framework and offers performance-robust partitioning of work over a wide range of core counts.
許多其餘經常使用的面向對象的編程語言,包括那些託管在JVM(例如Groovy,Ruby,和Scala),以及託管在其餘虛擬機(C#在CLR),都包含閉包功能。所以,Java程序員也愈來愈熟悉的閉包的語言功能以及它的編程模型。
特別使人感冒的是內部迭代語句的特性,數組和集合目前支持外部迭代,就是控制迭代的邏輯代碼在數據結構外部,從而實現數據的迭代。例如,for-each迭代數組和集合就是這樣一個外部迭代的例子。Java中for-each迭代就是比較嚴格的串行迭代,這意味着,在不容許使用全部可用的核心庫的狀況下。程序員只能用一種方法來實現遍歷一個標準的集合。
使用內部迭代,數據結構就像lamdba表達式同樣執行代碼片斷,而且數據結構也負責分割計算並返回結果。由於數據結構和他本身的內部細節想似。他能夠經過調整選項,而後選擇最好的任務調度進行程序計算。就像:
修改執行順序
使用線程池同步執行
使用分區和工做竊取並行執行,The fork/join framework,在JavaSE 7中添加,是一個這樣的候選並行處理框架,工做性能可靠的分區多核心計數。
A prototypical example of the internal iteration style is a sequence of filter-map-reduce operations such as:
int maxFooWeight =
collection.filter( /* isFoo Predicate as a lambda /)
.map( / Map a Foo to its weight with a lambda /)
.max(); / Reduction step */
The lambda expressions are expressions with concise(簡明的) syntax(語法) to represent the desired operation. This style is in lieu(代替) of one or moreexplicit(明確的) for loops(環) which would unnecessary constrain(驅使) the iteration order over the collection. In addition, a well-designed algorithm(算法) can not onlyexecute(實行) these sets of operations in parallel, but can also aggregate(集合) the three operations into a single parallel pass.
Project Lambda also includes virtual(虛擬的) extension(延長) methods, which will address the long-standing limitation(限制) of not being able to add methods to widely-usedinterfaces(界面) because of source compatibility(兼容性) concerns(關係).
By adding extension methods to the existing collection interfaces, such asjava.util.Collection and java.util.List, existing implementations(實現) of those types can participate(參與) in the new programming idiom. Implementations of those types in the JDK (and elsewhere(在別處)) can override(推翻) the default implementations of the extension methods from the superinterfaces to provide higher-performance or otherwisespecialized(專業的) implementations.
Description
The in-progress OpenJDK project page for Project Lambda and corresponding JSRhave the most up-to-date(最新的) information about the details of the effort.
The platform components(成分) impacted(影響) by the overall Project Lambda effort include:
The specification(規格) of the Java programming language
The specification of the Java virtual(虛擬的) machine
The reference(參考) implementation(實現) of the language changes in javac
Class file changes to support compilation(編輯) of lambda expressions and virtual extension(延長) methods
(Possible) JVM enhancements(增長) to improve execution(執行) of lambdas
JVM changes to support virtual extension methods
Core API changes to add virtual extension methods
Core API changes to support use of lambda expressions
Updates to the JDK libraries to use the new extension methods
Updates to the reflective(反射的) APIs, such as core reflection(反射)andjavax.lang.model, to expose lambda and extension method related information
Updates to class file tools like javap and pack200/unpack200 to understand new JVM attributes(屬性)
(Possible) serialization(序列化) updates, including IIOP serialization
Enhancements(增長) to javadoc to indicate(代表) which interfaces(界面) can be used for lambdas
Language runtime in java.lang.* to support the translation of lambda expressions and virtual(虛擬的) extension(延長) methods
Alternatives
Project Lambda started with a straw-man proposal and has gone through several 「State of the Lambda」 iterations which have evolved(發展) both the syntax(語法) andfeature(特點) set of the project.
The work on lambda expression portion(部分) of Project Lambda is informed(通知) bynumerous(許多的) previous efforts, including but not limited to:
BGGA,
CICE
FCM
Pizza
The feature(特點) set of Project Lambda is judged to better address the needs ofevolving(發展) the Java platform than the earlier proposals(提議).
The design of virtual extension methods is likewise(一樣地) informed(通知) by a large body of prior(優先的) work in the programming language community including:
multiple inheritance(繼承) in C++
static extension(延長) methods in C#
traits in Fortress
traits in Scala
mixins in Strongtalk
Compared to other language environments, the Java language has always had much more predictable semantics(語義學), built-in(嵌入的) primitive(原始的) types are of a known size, exceptions(例外) are thrown at precise(精確的) locations, the order ofexecution(執行) of expressions is precisely defined(定義), and so on. Loosening(放鬆) the semantics of the built-in for and while loops(環) to allow auto-parallelization attempts was judged to be neither desirable(使人滿意的) nor sufficient(足夠的) to meet the goals of supporting multi-core.
Testing
In addition to unit/regression(迴歸) tests, as a new language feature(特點) JCK tests will also be developed.
The utility(實用) of the feature will also be validated(證明) through usage(使用) in the JDK platform libraries.
Risks and Assumptions
As a large effort touching many aspects(方面) of the platform, there is the possibility forunforeseen(未預見到的) complications(併發症) and interactions(相互做用) to arise.
By starting early and planning for several iterations(迭代) of, say, the supporting libraries work, the impact(影響) of those complications should be mitigated(緩和).
Dependences
The currently preferred implementation(實現) approach(方法) for lambda expressionsrelies(依靠) oninvokedynamic and method handles introduced by JSR 292. Therefore,acceptable(可接受的) performance of lambda depends on acceptable performance of method handles, amongst(在…之中) other factors(因素).
There is expected to be feedback(反饋) between the libraries work (JEPs 107 and 109) and the language features.
Impact
Other JDK components(成分): The impact of other JDK components isoutlined(概述) in the description section.
Compatibility(兼容性): Virtual(虛擬的) extension(延長) methods are intended to allow source compatible(兼容的) evolution(演變) of interfaces(界面).add virtual extension methods
Security: Security review will be needed on aspect of the runtime implementation of lambdas.
Performance/scalability(可擴展性): Performance of lambda-friendly idiomsversus(對) traditional idioms should be tracked.
Documentation(文件材料): User guides and supporting documentation needs to be written.
TCK: As a large platform feature(特點), language and library TCKs need to be developed.