小编Kin*_*ard的帖子

Netty 4 中导致 BlockingOperationException 的原因是什么?

最近BlockingOperationException在我的netty4项目中发现了一些。

\n\n

有人说使用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\n

我的项目代码如下:

\n\n
private 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)

java netty

2
推荐指数
1
解决办法
1346
查看次数

标签 统计

java ×1

netty ×1