Is Netty Channel.write thread safe?

I have a Netty application where I want to have more than one stream to write to a channel. I'm just wondering if Channel.write is thread safe?

+5
source share
3 answers

As you can see from the code, the method itself is ChannelOutboundBuffer.addMessage()not thread safe. However, the write channel is "thread safe" because netty executes the write task / method in a single I / O stream.

+3
source

It is thread safe, so you don’t need to worry.

+2
source

, , Channel.write ChannelOutboundBuffer.addMessage HeadContext 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);
 }
0

All Articles