netty提供了三种方式和写相关:
(1) write(msg) ⇒ pass through pipeline
(2) flush()=> gathering write of previous written msgs
(3) writeAndFlush()=>short-cut for write(msg) and flush()
通过前文的分析可知,写并没有真正的去发送给远端,而是缓存起来。而flush可以理解为开始写。所以在实际使用netty(包括netty的案例)时,都很简单的使用了writeAndFlush(),简单明了。
所以服务器在发送数据时,则面对这样的情况:
write 1-> flush 1
write 2-> flush 2
write 3-> flush 3
netty作者提及作为最佳实践应该“Limit flushes as much as possible as syscalls are quite expensive.” 继续阅读netty analyst(3)-write performance improvement