[转]Netty进阶之路--ChannelHandler并发安全、NioEventLoop防阻塞挂死
《Netty进阶之路》第7章、第8章分别提出ChannelHandler并发安全问题,NioEventLoop线程阻塞导致消息接收和处理缓慢。ChannelHandler的并发安全问题很好分析,NioEventLoop线程阻塞则需要一些技巧。 1 ChannelHandler并发安全 默认每个Channel中有各自的ChannelHandler实例,因此如果所有业务在NioEventLoop中完成则没有线程安全问题,因为从消息接收到响应写出阶段都由同一个NioEventLoop线程处理;若在ChannelHandler消息处理过程中新开启一个线程,则新线程和NioEventLoop同时读写ChannelHandler中的变量时会有线程不安全的风险。 @Component(value = "gatewayChannelInitializer") public class GatewayChannelInitializer extends ChannelInitializer { private static final String ENCODER = "encoder"; private static final String DECODER = "decoder"; @Autowired private BusyMan busyMan; protected void