Netty Channel.write线程安全吗?

时间:2012-06-19 05:44:46

标签: java thread-safety netty channel

我有一个Netty应用程序,我希望有一个以上的线程写入一个频道。我只是想知道Channel.write是否是线程安全的?

3 个答案:

答案 0 :(得分:5)

从代码中可以看出,ChannelOutboundBuffer.addMessage()方法本身不是线程安全的。但是,写入通道是“线程安全的”,因为netty在单个I / O线程中执行写入任务/方法。

答案 1 :(得分:2)

它是线程安全的,所以你不必担心。

答案 2 :(得分:0)

不,它的线程不安全,因为Channel.write在其管道的HeadContext中调用了ChannelOutboundBuffer.addMessage,而ChannelOutboundBuffer.addMessage肯定是线程不安全的。看看这段代码:

 public void addMessage(Object msg, int size, ChannelPromise promise) {
     Entry entry = Entry.newInstance(msg, size, total(msg), promise);
     if (tailEntry == null) {
         flushedEntry = null;
         tailEntry = entry;
     } else {
         Entry tail = tailEntry;
         tail.next = entry;
         tailEntry = entry;
     }
     if (unflushedEntry == null) {
         unflushedEntry = entry;
     }

     // increment pending bytes after adding message to the unflushed arrays.
     // See https://github.com/netty/netty/issues/1619
     incrementPendingOutboundBytes(size, false);
 }
相关问题