JAVA912新特性简述

11次阅读

共计 3323 个字符,预计需要花费 9 分钟才能阅读完成。

本文是个人在企业内部分享使用的简要大纲,列举了 JAVA9-12 的重要更新,文章的结构较简单,也不规范,鉴于近期写若干文章时总会忘记一些新特性所处的版本,特将此大纲流 copy 留用。

一 JAVA9 新特性

1.Java Platform Module System/ Modular Java Application Packaging 模块化系统及类加载器分级

2.jshell

3.Multi-Release JAR Files 多版本的 jar 包

4.Jlink 工具

5.Segmented Code Cache 代码片段缓存

6.Dynamic Linking of Language-Defined Object Models 动态链接语言定义模型

7.Unified JVM Logging 统一 JVM 日志

8. 将 G1 作为默认垃圾收集器, 并 deprecated CMS.

9. 进程 API

10. 变量句柄

11. 字符串内部实现优化 (字符数组变字节数组 + 编码方式)

12. 发布订阅框架

13. 集合工厂方法

14. 自旋暗示

15. 对象序列化数据过滤

16.” 栈旅行者 ”

17.Milling Project Coin(鬼知道该怎么翻译).

Allow @SafeVargs on private instance methods. // 允许在私有实例方法上标注 @SafeVargs 注解

Allow effectively final variables to be used as resources in the try-with-resourcesstatement. //try-with-resource 语法支持实际 final 的变量

Allow the diamond with anonymous classes if the argument type of the inferred type is denotable.// 允许对可推荐出类型的匿名内部类使用钻石符号.

Complete the removal, begun in Java SE 8, of the underscore from the set of legal identifier names.// 移除 java8 中已经开始的, 以下划线作为完整标识符名称

Add support for private interface methods.// 接口中可定义私有方法.

18. 扩大的 unicode 集

19. 孵化官方 http client.

二 JAVA10

1. 局部变量类型推断

2. 统一定义的 GC 接口.

3.g1 垃圾收集器的并行 full gc

4. 应用类数据共享

5.thread local 握手

6. 扩展 unicode 语言标签.

三. JAVA11

1.unicode10 支持

2. 标准化 java9 中孵化的 http 客户端

demo: 同步式编程

public void get(String uri) throws Exception {

    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(uri))
            .build();
    HttpResponse<String> response =
            client.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response.body());
}

demo: 响应式编程

public CompletableFuture<String> getCompletableFuture(String uri) {

    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(uri))
            .build();

    return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
            .thenApply(HttpResponse::body);
}

demo: 响应式编程 - 并发

public void testConcurrentRequests(){

    HttpClient client = HttpClient.newHttpClient();
    List<String> urls = List.of("http://www.baidu.com","http://www.alibaba.com/","http://www.tencent.com");
    List<HttpRequest> requests = urls.stream()
            .map(url -> HttpRequest.newBuilder(URI.create(url)))
            .map(reqBuilder -> reqBuilder.build())
            .collect(Collectors.toList());

    List<CompletableFuture<HttpResponse<String>>> futures = requests.stream()
            .map(request -> client.sendAsync(request, HttpResponse.BodyHandlers.ofString()))
            .collect(Collectors.toList());
    futures.stream()
            .forEach(e -> e.whenComplete((resp,err) -> {if(err != null){err.printStackTrace();
                }else{System.out.println(resp.body());
                    System.out.println(resp.statusCode());
                }
            }));
    CompletableFuture.allOf(futures
            .toArray(CompletableFuture<?>[]::new))
            .join();}

注:以上代码是分享时从网上转来的,因为此文是从内部 wiki 粘过来,出处已忘。接下来打算发一篇 CompletableFuture 有关的文章,最近对响应式编程的兴趣更加深刻了。

3.Collection.toArray(IntFunction) Default 方法

4. 编译器线程的懒分配

5. 体验版 zgc 大杀器 (缺少类和元数据卸载)

6.Epsilon GC no-op 体验版

7. 少代价的堆分析工具 jvmti

8.nests, an access-control context , 省去编译器插入 accessibility-broadening bridge methods

9. 一些属性变事实的只读.java.home, user.home, user.dir, user.name 等

10.Reference 类不再支持克隆

11. 使用 classpath 进行编译运行时, 默认解析的模块策略变更:root 模块集在此版本变更为所有可见的导出 api 的系统模块, 唯一可见的改变是 java.se 模块不在默认解析.

12.SelectableChannel 可以在 select 操作正在进行中注册.

13.DatagramChannel.send Throws AlreadyConnectedException Instead of IllegalArgumentException

14. 为并发 gc 提供新的性能记数器

15.g1 支持自适应的引用处理时的线程数. 所有 gc 支持 stw 阶段的并行自适应.

16. 模块路径下支持类数据共享 cds.

四.JAVA12

1.unicode11 支持

2.jvm 常量 api

3. 简写的数字格式:1k

4.zgc 支持并发类数据卸载

5. 支持交替在内存设备上分配老年代, 支持相应功能的设备如 ” 非易变 - 双列直插式内存模型 ”(NV-DIMM).

6.switch case 支持语句的同时支持表达式.case … ->

7. 更好的支持 http 重定向 (HttpURLConnection)

8.g1 可能在并发标记周期交回内存.

9 可终止的 g1 混合 gc

10. 让 g1 在空闲时自动交回已提交但未使用的内存.

11.Shenandoah 低停顿 gc 体验版.

正文完
 0