點擊上方“程式員大咖”,選擇“置頂公眾號”
關鍵時刻,第一時間送達。
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/
程式員大咖整理髮布,轉載請聯絡作者獲得授權
【點擊成為安卓大神】