如果还有其它类似值得优化的注意点,欢迎评论留言,多多交流!!!
相关版本:jdk1.8.0_201版本 jdk17.0.2
jdk1.8.0_201代表该版本还存在对应的问题,不代表问题从当前版本开始。
jdk17.0.2代表已经修复或者优化,不代表从该版本开始优化。
- 在获取异步返回值时,应该调用CompletableFuture#get(long timeout, TimeUnit)代替CompletableFuture#get(),如果要无限期等待可以将timeout设置成Integer.MAX_VALUE.
原因请查看:
jdk issue:CompletableFuture.waitingGet shouldn’t call Runtime.availableProcessors in a tight loop
jdk issue:[linux] Runtime.availableProcessors 执行时间增加了 100 倍
目前我看的jdk17中已经修复这个问题.关于该问题参考如下文章,分析的特别好:https://baijiahao.baidu.com/s?id=1724155491094719142&wfr=spider&for=pc ConcurrentHashMap#computeIfAbsent
ConcurrentHashMap#computeIfAbsent(K key, Function super K, ? extends V> mappingFunction)出现相同hash值时会加锁。在加锁之后才会校验key是否相同,大量重复key的,并发时可能影响性能。在jdk17中会先校验key是否相同。
jdk issue:ConcurrentHashMap.computeIfAbsent(k,f) locks bin when k present
mybatis pr:Prevent thread from being blocked by JDK-8 ConcurrentHashMap#computeIfAbsent()
mybatis issue#2297



