Java8 新特性:Lambda表达式和虚拟扩展方法标注

772 查看

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 8添加了Lambda表达式(闭包)和特性支持,包括方法的引用,增强类型推断,和虚拟扩展方法。

Goals

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语言的一整套辅助支持特性,起到促进一些平台的目的:

·使得更加抽象,性能更高的库创建和消费起来更简单
·支持平滑的演进库与兼容性迁移

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.

我的博客ENUE.CN