ChannelOutboundInvoker.flush()是同步/阻止调用吗?

时间:2020-06-24 05:56:59

标签: netty

在调用ChannelOutboundInvoker.flush()ChannelOutboundInvoker.writeAndFlush()之后直接重用ByteBuf(包装的字节数组)是否安全?

byte[] bytes = ...
ByteBuf byteBuf = Unpooled.wrappedBuffer(bytes);
ctx.writeAndFlush(byteBuf);
bytes[0] = 0; // safe to do here?

我已经尝试了数十次,每次都很好,但是我想知道是否保证可以。

1 个答案:

答案 0 :(得分:1)

不,这不安全/不正确,因为不能保证立即执行。唯一“安全”的方法是在byte[]返回的ChannelFuture完成后重新使用writeAndFlush(...)

类似这样的东西:

byte[] bytes = ...
ByteBuf byteBuf = Unpooled.wrappedBuffer(bytes);
ctx.writeAndFlush(byteBuf).addListener(f -> {
    bytes[0] = 0;
});
相关问题