[英]Why does netty have its own ConcurrentHashMap?
I noticed that Netty has some internal Concurrent HashMap utilities. 我注意到Netty有一些内部并发HashMap实用程序。 I'm curious why Netty doesn't use the ConcurrentHashMap that's built into the Java Core.
我很好奇为什么Netty不使用Java Core内置的ConcurrentHashMap。 Is the Netty implementation better in some way, or does it have some new functionality?
Netty实施是否在某种程度上更好,还是具有一些新功能? I'm working on a project that needs a Concurrent HashMap and I'm debating whether I should use the netty implementation, but I can't see any difference in the source code.
我正在一个需要并发HashMap的项目中,正在辩论是否应该使用netty实现,但是我看不出源代码有什么不同。
ConcurrentHashMap
didn't exist until JSR-166 , which was released in Java 5 as the java.util.concurrent
package. 直到JSR-166才存在
ConcurrentHashMap
,该JSR-166在Java 5中作为java.util.concurrent
软件包发布。
Netty doesn't include their own ConcurrentHashMap
because it's superior - in fact, it's certainly just a copy of JSR-166 - it's so they can run on Java 1.4. Netty不包括其自己的
ConcurrentHashMap
因为它具有优越的性能-实际上,它当然只是JSR-166的副本-因此它们可以在Java 1.4上运行。
For your own projects, you should just use java.util.concurrent.ConcurrentHashMap
if you can take a dependency on Java 5. And if you can't, then you should just include it in your product (and change the package name so that it doesn't conflict with the Java 5 runtime's included projects.) Any time you can get Doug Lea or Brian Goetz to write your thread-safe code for you, you probably should. 对于您自己的项目,如果可以依赖Java 5,则应该仅使用
java.util.concurrent.ConcurrentHashMap
如果不能,则应将其包含在产品中(并更改包名称,以便它与Java 5运行时包含的项目没有冲突。)任何时候,您都可以让Doug Lea或Brian Goetz为您编写线程安全代码。
Netty's ConcurrentHashMap is actually buggy; Netty的ConcurrentHashMap实际上是越野车。 it is not threadsafe.
它不是线程安全的。
I found that recently. 我最近发现的。 You can prove it by a simple test.
您可以通过简单的测试来证明这一点。
We had: thread1 loops over map.values().stream().sorted().findFirst() thread2 concurrenly put/remove from the map. 我们有:thread1循环遍历map.values()。stream()。sorted()。findFirst()thread2同时从地图中放置/删除。
By design any form of iteration should not necessarily reflect the map, but it should never fail with ArrayIndexOutOfBoundsException in some deep and obscure code unless the netty version messed up the map data while the jdk streams stuff relies on it. 通过设计,任何形式的迭代都不必一定反映地图,但是它绝不应该在某些晦涩难懂的代码中以ArrayIndexOutOfBoundsException失败,除非netty版本弄乱了地图数据,而jdk流中的内容依赖于此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.