简体   繁体   English

为什么netty有自己的ConcurrentHashMap?

[英]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.

相关问题 线程在ConcurrentHashMap中是否将新值放入其自己的段中 - Does a thread put a new value in its own segment in ConcurrentHashMap ConcurrentHashMap上的Netty ChannelGroup - Netty ChannelGroup over ConcurrentHashMap 为什么必须从自己的类中启动JavaFx应用程序? - Why does a JavaFx Application HAVE to be launched from within its own class? HashMap为什么以及如何有自己的hashCode()内部实现,称为hash()? - Why and how does HashMap have its own internal implementation of hashCode() called hash()? 为什么Android有自己的URI实现并且不使用默认的Java实现? - Why does Android have its own URI implementation and doesn't use the default Java implementation? 为什么 ConcurrentHashMap 不能为每个桶都加锁? - Why ConcurrentHashMap cannot have a lock for each bucket? 为什么ConcurrentHashMap在双重检查锁定中起作用 - Why does ConcurrentHashMap work in Double Checked Locking 为什么ConcurrentHashMap不将地图的大小存储在AtomicInteger中? - Why does ConcurrentHashMap not store the size of the map in a AtomicInteger? 为什么 ConcurrentHashMap 防止空键和值? - Why does ConcurrentHashMap prevent null keys and values? 每个JSpinner对象是否都有自己的Model对象? - Does each JSpinner object have its own Model object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM