问题描述
在netty Java文档中说so be sure to call flush() once you want to request to flush
,但是在此示例中说没有必要调用ctx.flush().但是我没有发现flush(ChannelHandlerContext ctx)
像它所说的那样.
In the netty Java document says that so be sure to call flush() once you want to request to flush
, but in this example it says that there is no need to call ctx.flush(). But I did not find flush(ChannelHandlerContext ctx)
was called as what it said.
package io.netty.example.time;
public class TimeEncoder extends ChannelOutboundHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
UnixTime m = (UnixTime) msg;
ByteBuf encoded = ctx.alloc().buffer(4);
encoded.writeInt((int)m.value());
ctx.write(encoded, promise); // (1)
}
}
Second, we did not call ctx.flush(). There is a separate handler method void flush(ChannelHandlerContext ctx) which is purposed to override the flush() operation.
https://netty.io/wiki/user-guide-for-4.x.html#wiki-h3-8
Second, we did not call ctx.flush(). There is a separate handler method void flush(ChannelHandlerContext ctx) which is purposed to override the flush() operation.
https://netty.io/wiki/user-guide-for-4.x.html#wiki-h3-8
推荐答案
缓冲的要点是,您可以将一堆"数据聚集"在一起并一次发送,因为发送大量数据的速度大致一样快因为要发送少量数据(因为IO速度很慢).
The point of buffering is that you "gather" a bunch of data together and send it all at once, because it's roughly as fast to send a lot of data as it is to send a small amount of data (because IO is slow).
这正在分配一个4字节的缓冲区.如果每4个字节刷新一次,那将不是很有效.
This is allocating a 4-byte buffer. If you flushed every 4 bytes, it wouldn't be very efficient.
在某处会有冲洗,只是不是以这种特定方法进行的.
There will be a flush, somewhere, just not in this particular method.
这篇关于为什么在Netty用户指南中调用ctx.write()之后没有调用ctx.flush()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!