Question

I have a question about concurrency in upstream/downstream handlers of a channel pipe. I always thought that if a new handler is created when the pipe is constructed (that is, the handler isn't shared amongst the pipes), at most one thread interacts with the handler.

Now, I was browsing through the examples. More specific, take this one: http://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/discard/DiscardServerHandler.html

In the code, there is a member variable (it is used to count the total number of bytes):

private final AtomicLong transferredBytes = new AtomicLong();

Why do they use a AtomicLong here? The handler is constructed as following (see http://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/discard/DiscardServer.html):

bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    public ChannelPipeline getPipeline() throws Exception {
        return Channels.pipeline(new DiscardServerHandler());
    }
 });

So, the handler is not shared. I cannot find a reason why they would want to use a AtomicLong, instead of a plain long, here. Can somebody explain?

Thanks!

Was it helpful?

Solution

Its a "bug" in the example. There is no need for the AtomicLong here. So you are right. If you add a new instance of the handler on every ChannelPipeline creation you should not need to bother with this kind of concurrency issues.

I will fix the example in netty. Thanks!

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