【发布时间】:2012-01-09 04:56:54
【问题描述】:
我对通道管道的上游/下游处理程序中的并发性有疑问。我一直认为,如果在构建管道时创建了一个新的处理程序(即处理程序不在管道之间共享),那么最多有一个线程与处理程序进行交互。
现在,我正在浏览示例。更具体的,拿这个:http://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/discard/DiscardServerHandler.html
代码中有一个成员变量(用来统计总字节数):
private final AtomicLong transferredBytes = new AtomicLong();
为什么他们在这里使用 AtomicLong?处理程序的构造如下(参见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());
}
});
因此,处理程序不共享。我找不到他们为什么要在这里使用 AtomicLong 而不是普通 long 的原因。谁能解释一下?
谢谢!
【问题讨论】:
标签: concurrency netty