在此次JDK12中,共更新了8个特性,下面对新特性做介绍。JDK12官方说明
八大新特性
一.添加Shenandoah:一个低暂停时间的垃圾收集器(实验特性):
官方解释:
Summary:Add a new garbage collection (GC) algorithm named Shenandoah which reduces GC pause times by doing evacuation work concurrently with the running Java threads. Pause times with Shenandoah are independent of heap size, meaning you will have the same consistent pause times whether your heap is 200 MB or 200 GB.
概要:添加一个名为Shenandoah的新垃圾收集(GC)算法,该算法通过与正在运行的Java线程同时进行撤离工作来减少GC暂停时间。Shenandoah的暂停时间与堆大小无关,这意味着无论您的堆是200 MB还是200 GB,您都将具有相同的一致暂停时间。
Success Metrics:This project will be a success if we can keep consistent short gc pause times.
成功指标:如果我们能够保持一致的gc暂停时间,则该项目将是成功的。
二.微基准测试套件(Microbenchmark Suite):
官方解释:
Summary:Add a basic suite of microbenchmarks to the JDK source code, and make it easy for developers to run existing microbenchmarks and create new ones.
概要:在JDK源代码中添加基本的微基准套件,使开发人员可以轻松地运行现有的微基准并创建新的微基准。
Goals:
1.Based on the [Java Microbenchmark Harness (JMH)]
2.Stable and tuned benchmarks, targeted for continuous performance testing
①.A stable and non-moving suite after the Feature Complete milestone of a feature release, and for non-feature releases
②.Support comparison to previous JDK releases for applicable tests
3.Simplicity
①.Easy to add new benchmarks
②.Easy to update tests as APIs and options change, are deprecated, or are removed during development
③.Easy to build
④.Easy to find and run a benchmark
4.Support JMH updates
5.Include an initial set of around a hundred benchmarks in the suite
目标:
1.基于Java Microbenchmark Harness(JMH)
2.稳定且经过调整的基准,可用于连续性能测试
①.在功能完整的里程碑(适用于功能发行)和适用于非功能的发行版之后的稳定且不可移动的套件
②.与以前的JDK版本的支持比较以进行适用的测试
3.简单
①.轻松添加新基准
②.在开发过程中,随着API和选项的更改,不推荐使用或删除,易于更新测试
③.易于建造
④.易于查找和运行基准
4.支持JMH更新
5.套件中包含约一百个基准的初始集合
概念:JMH,即Java Microbenchmark Harness,是专门用于代码微基准测试的工具套件。何谓Micro Benchmark呢?简单的来说就是基于方法层面的基准测试,精度可以达到微秒级。当你定位到热点方法,希望进一步优化方法性能的时候,就可以使用JMH对优化的结果进行量化的分析。
JMH比较典型的应用场景有:
- 想准确的知道某个方法需要执行多长时间,以及执行时间和输入之间的相关性.
- 对比接口不同实现在给定条件下的吞吐量.
- 查看多少百分比的请求在多长时间内完成.
三.表达式switch(预览特性):
switch新特性:使用lamda表达式进行替换。
public class SwitchTest {
@Test
public void test1(){
Week day=Week.FRIDAY;
switch (day){
case MONDAY:
case TUESDAY:
case WEDNESDAY:
System.out.println("星期一或星期二或星期三");
case THURSDAY:
case FRIDAY:
System.out.println("星期四或星期五");
case SATURDAY:
System.out.println("星期六");
case SUNDAY:
System.out.println("星期日");
default:
throw new IllegalStateException("What day is it today"+day);
}
}
//jdk12新特性
public void test2(){
Week day=Week.FRIDAY;
switch (day){
case MONDAY,TUESDAY,WEDNESDAY->System.out.println("星期一或星期二或星期三");
case THURSDAY,FRIDAY->System.out.println("星期四或星期五");
case SATURDAY->System.out.println("星期六");
case SUNDAY->System.out.println("星期日");