Netty Channel.write线程安全吗?

问题描述 投票:5回答:3

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

java thread-safety netty channel
3个回答
5
投票

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


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);
 }
© www.soinside.com 2019 - 2024. All rights reserved.