【Chapter 1 Netty and Java NIO APIs】
The entire Netty API is asynchronous.安全
Asynchronous processing encourages you to use your resources more efficiently by allowing you to start a task and get notified when it's done rather than waiting for it to complete. You're free to do something else while the task is running.app
实现异步的两种方法:异步
On Linux-like OSs the selector makes use of the epoll- IO event notification facility. This is a high-performance technique in which the OS works asynchronously with the networking stack.Unfortunately, even today the famous epoll- bug can lead to an invalid state in the
selector, resulting in 100% CPU-usage and spinning.async
【Chapter 2 Your first Netty application】ide
Handler能够被多个Channel共享,这时Handler必须被@Sharable注解且必须是线程安全的this
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER) //writeAndFlush是异步操做,返回ChannelFuture .addListener(ChannelFutureListener.CLOSE); //经过ChannelFuture.addListener()注册监听器,当Future.isDone()等于true时会执行监听器
对于一个Channel,其pipeline中至少要有一个Handler可以处理异常线程
Next, override the channelRead0() method. The method is called once data is received.Note that the bytes may be fragmented, which means that if the server writes 5 bytes it s not guaranteed that all 5 bytes will be received at once. For 5 bytes, the channelRead0() method could be called twice, for example. The first time it may be called with a ByteBuf that holds 3 bytes and the second time with a ByteBuf that holds 2 bytes. The only guarantee is that the bytes will be received in the same order as they re sent. But this is only true for TCP or other stream-orientated protocols.code
SimpleChannelInboundHandler与ChannelInboundHandlerAdapter的区别:orm