Question

I noticed that Netty has some internal Concurrent HashMap utilities. I'm curious why Netty doesn't use the ConcurrentHashMap that's built into the Java Core. Is the Netty implementation better in some way, or does it have some new functionality? 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.

Was it helpful?

Solution

ConcurrentHashMap didn't exist until JSR-166, which was released in Java 5 as the java.util.concurrent package.

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.

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.

OTHER TIPS

Netty's ConcurrentHashMap is actually buggy; 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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top