最近BlockingOperationException在我的netty4项目中发现了一些。
有人说使用netty的ServerBootstrap的sync()方法启动时会导致死锁,因为sync()会调用await()方法,而await()中有一个叫“checkDeadLock”的方法。
\n\n但我不这么认为。ServerBootstrap使用的是名为boosGroup的EventLoopGroup,而Channel使用的是workerGroup来操作IO,我认为他们不会互相影响,他们有不同的EventExecutor。
\n\n而在我的实践中,Netty启动过程中并没有出现死锁异常,大部分发生在Channel的await writeAndFlush之后。
\n\n分析源码,,checkDeadLock抛出BlockingOperationException的异常是当当前执行的线程和executor线程是同一个执行的时候。
我的项目代码如下:
\n\nprivate void channelWrite(T message) {\n boolean success = true;\n boolean sent = true;\n int timeout = 60;\n try {\n ChannelFuture cf = cxt.write(message);\n cxt.flush();\n if (sent) {\n success = cf.await(timeout);\n }\n if (cf.isSuccess()) {\n logger.debug("send success.");\n }\n Throwable cause = cf.cause();\n if (cause != null) {\n this.fireError(new PushException(cause));\n }\n } catch (LostConnectException e) {\n this.fireError(new PushException(e));\n } catch (Exception e) {\n …Run Code Online (Sandbox Code Playgroud)