lare96
lare96

Reputation: 33

Netty - Using the EventLoop to combine operations?

I came across this suggestion to combine operations by using the underlying channel's EventLoop to execute them together. In the example provided, write operations are used. It was my assumption was that write operations were already done asynchronously on an I/O thread (the EventLoop?) provided by Netty... so why is it necessary to execute multiple operations using the EventLoop the channel is assigned to?

Also, if doing operations on the EventLoop thread provides some sort of performance benefit then why does that only apply to multiple operations and not single ones?

Any clarification would be appreciated! Thanks.

Upvotes: 1

Views: 1187

Answers (1)

As I understand when you call this code outside of EventLoop:

channel.write(msg1);
channel.writeAndFlush(msg3);

It under the hood transforms to:

channel.eventLoop().execute(new Runnable() {
  @Override
  public void run() {
    channel.write(msg1);
  }
});
channel.eventLoop().execute(new Runnable() {
  @Override
  public void run() {
    channel.writeAndFlush(msg3);
  }
});

So for reducing dispatching overhead it is better to combine them to one:

channel.eventLoop().execute(new Runnable() {
  @Override
  public void run() {
    channel.write(msg1);
    channel.writeAndFlush(msg3);
  }
});

Upvotes: 2

Related Questions