Java 8 和 Java 9 中并发工具的改变



来源:鸟窝,

colobu.com/2018/03/12/Concurrency-Utilities-Enhancements-in-Java-8-Java-9/


Java 8 和 Java 9中 concurrent 包有了一些改变, 本文对这些改变做了汇总。


Java 8 中 Concurrent package的改变


java.util.concurrent中新的类和接口


增加了两个新接口和4个新类:


  • 接口 CompletableFuture.AsynchronousCompletionTask

  • 接口 CompletionStage

  • 类 CompletableFuture

  • 类 ConcurrentHashMap.KeySetView

  • 类 CountedCompleter

  • 类 CompletionException


java.util.concurrent.ConcurrentHashMap的新方法


集合框架 在Java 8中做了修订,基于 stream 和 lambda表达式 添加了很多聚合方法。因此 ConcurrentHashMap 也引入了30几个新方法,包括各种 foreach 方法(forEach , forEachKey , forEachValue , 和 forEachEntry )、搜索方法( search , searchKeys , searchValues , 和 searchEntries )和reduction方法( reduce ,reduceToDouble , reduceToLong 等)。


也添加了一些其它方法,比如 mappingCount 和 newKeySet 。并且当前版本的 ConcurrentHashMap 的更适合做cache,因为增加了当键值不存在的时候的检查方法。


java.util.concurrent.atomic中的新类


为了并发计算count、sum, 新引入了 DoubleAccumulator , DoubleAdder , LongAccumulator , LongAdder 类,比Atomic提供更高的吞吐率。


java.util.concurrent.ForkJoinPool的新方法


静态的 commonPool() 新加入,可以为ForkJoinTask提供通用池。


两个方法 getCommonPoolParallelism() 和 commonPool() 提供不同的配置。


新类 java.util.concurrent.locks.StampedLock


新类 StampedLock 提供三种模式(写,读,乐观读),用来提高性能。


Java 9 中 Concurrent package的改变


主要是 JEP 266: More Concurrency Updates , 包括publish-subscribe, CompletableFuture 接口的加强等。


  • 支持Reactive Streams publish-subscribe框架,四个接口 Processor 、 Publisher 、 Subscriber 、 Subscription ,容器类 java.util.concurrent.Flow 、java.util.concurrent.SubmissionPublisher

  • CompletableFuture类加强,支持delays, timeout, subclassing 以及其它方法

  • 调优以及修改javadoc


参考文档


  • Concurrency Utilities Enhancements in Java SE 8

    https://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/changes8.html

  • Java 9′s other new enhancements, Part 6: Concurrency

    https://www.javaworld.com/article/3198904/learn-java/java-9s-other-new-enhancements-part-6-concurrency.html

  • The Essential Java 9 Feature You Probably Never Heard Of

    https://blog.takipi.com/the-essential-java-9-feature-you-probably-never-heard-of/

  • Java 9′s other new enhancements

    https://www.infoworld.com/article/3169289/java/java-9s-other-new-enhancements-part-2.html

  • JEP 266: More Concurrency Updates

    http://openjdk.java.net/jeps/266

  • Java 9 series: Concurrency Updates

    https://www.voxxed.com/2016/10/java-9-series-concurrency-updates/


Java 8 和 Java 9中 concurrent 包有了一些改变, 本文对这些改变做了汇总。

来源:鸟窝,

colobu.com/2018/03/12/Concurrency-Utilities-Enhancements-in-Java-8-Java-9/


Java 8 和 Java 9中 concurrent 包有了一些改变, 本文对这些改变做了汇总。


Java 8 中 Concurrent package的改变


java.util.concurrent中新的类和接口


增加了两个新接口和4个新类:


  • 接口 CompletableFuture.AsynchronousCompletionTask

  • 接口 CompletionStage

  • 类 CompletableFuture

  • 类 ConcurrentHashMap.KeySetView

  • 类 CountedCompleter

  • 类 CompletionException


java.util.concurrent.ConcurrentHashMap的新方法


集合框架 在Java 8中做了修订,基于 stream 和 lambda表达式 添加了很多聚合方法。因此 ConcurrentHashMap 也引入了30几个新方法,包括各种 foreach 方法(forEach , forEachKey , forEachValue , 和 forEachEntry )、搜索方法( search , searchKeys , searchValues , 和 searchEntries )和reduction方法( reduce ,reduceToDouble , reduceToLong 等)。


也添加了一些其它方法,比如 mappingCount 和 newKeySet 。并且当前版本的 ConcurrentHashMap 的更适合做cache,因为增加了当键值不存在的时候的检查方法。


java.util.concurrent.atomic中的新类


为了并发计算count、sum, 新引入了 DoubleAccumulator , DoubleAdder , LongAccumulator , LongAdder 类,比Atomic提供更高的吞吐率。


java.util.concurrent.ForkJoinPool的新方法


静态的 commonPool() 新加入,可以为ForkJoinTask提供通用池。


两个方法 getCommonPoolParallelism() 和 commonPool() 提供不同的配置。


新类 java.util.concurrent.locks.StampedLock


新类 StampedLock 提供三种模式(写,读,乐观读),用来提高性能。


Java 9 中 Concurrent package的改变


主要是 JEP 266: More Concurrency Updates , 包括publish-subscribe, CompletableFuture 接口的加强等。


  • 支持Reactive Streams publish-subscribe框架,四个接口 Processor 、 Publisher 、 Subscriber 、 Subscription ,容器类 java.util.concurrent.Flow 、java.util.concurrent.SubmissionPublisher

  • CompletableFuture类加强,支持delays, timeout, subclassing 以及其它方法

  • 调优以及修改javadoc


参考文档


  • Concurrency Utilities Enhancements in Java SE 8

    https://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/changes8.html

  • Java 9′s other new enhancements, Part 6: Concurrency

    https://www.javaworld.com/article/3198904/learn-java/java-9s-other-new-enhancements-part-6-concurrency.html

  • The Essential Java 9 Feature You Probably Never Heard Of

    https://blog.takipi.com/the-essential-java-9-feature-you-probably-never-heard-of/

  • Java 9′s other new enhancements

    https://www.infoworld.com/article/3169289/java/java-9s-other-new-enhancements-part-2.html

  • JEP 266: More Concurrency Updates

    http://openjdk.java.net/jeps/266

  • Java 9 series: Concurrency Updates

    https://www.voxxed.com/2016/10/java-9-series-concurrency-updates/


Java 8 和 Java 9中 concurrent 包有了一些改变, 本文对这些改变做了汇总。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值